簡介:這是大圖片產生縮圖 導致imagecreatefromjpeg 記憶體崩潰問題的詳細頁面,介紹了和php,有關的知識、技巧、經驗,和一些php源碼等。
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=330430' scrolling='no'>
當圖片超過1M時就可能出現以下錯誤 當然這個也跟你php.ini設定有關 如果你php設定裡 memory_limit 16M 這個過小的話就會出現下面這個錯誤!
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 3456 bytes) in
解決方案
ini_set("memory_limit", "60M");
在 imagecreatefromjpeg 前動態設定大小 以解決記憶體不足問題
有的伺服器可能限制了這個函數的使用 ini_set() 這樣的話就會既不報錯 也無法產生縮圖
所以只有聯絡伺服器那邊手動把php.ini修改一下
相關參考
php 中,imagecreatefromjpeg 將在記憶體中建立一個位元影像資料來操作圖片,
這需要大量的內容。一個長寬各為2000的24位元影像片,至少需要 2000 x 2000 x (24/8) = 12M的
/***************** 這個計算大小的公式不知道是否準確 ****************************/
/***************** 這裡還有老外寫的一個 上傳時轉換圖片的函數 可以參考下 **********************/
Last night I posted the following note under move_upload_file and looked tonight to see if it got into the system. I happen to also pull up imagecreatefromjpeg and got reminded of many resize scripts in this section. Unfortunately, my experience was not covered by most of the examples below because each of them assumed less than obvious requirements of the server and browser. So here is the post again under this section to help others uncover the mystery in file uploading and resizing arbitrary sized images. I have been testing for several days with hundreds of various size images and it seems to work well. Many additional features could be added such as transparency, alpha blending, camera specific knowledge, more error checking. Best of luck.
--- from move_upload_file post ---
I have for a couple of years been stymed to understand how to effectively load images (of more than 2MB) and then create thumbnails. My note below on general file uploading was an early hint of some of the system default limitations and I have recently discovered the final limit I offer this as an example of the various missing pieces of information to successfully load images of more than 2MB and then create thumbnails. This particular example assumes a picture of a user is being uploaded and because of browser caching needs a unique number at the end to make the browser load a new picture for review at the time of upload. The overall calling program I am using is a Flex based application which calls this php file to upload user thumbnails.
The secret sauce is:
1. adjust server memory size, file upload size, and post size
2. convert image to standard formate (in this case jpg) and scale
The server may be adjusted with the .htaccess file or inline code. This example has an .htaccess file with file upload size and post size and then inline code for dynamic system memory.
htaccess file:
php_value post_max_size 16M
php_value upload_max_filesize 6M
<?php
// $img_base = base directory structure for thumbnail images
// $w_dst = maximum width of thumbnail
// $h_dst = maximum height of thumbnail
// $n_img = new thumbnail name
// $o_img = old thumbnail name
function convertPic($img_base, $w_dst, $h_dst, $n_img, $o_img){
ini_set('memory_limit', '100M'); // handle large images
unlink($img_base.$n_img); // remove old images if present
unlink($img_base.$o_img);
$new_img = $img_base.$n_img;
$file_src = $img_base."img.jpg"; // temporary safe image storage
unlink($file_src);
move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_src);
list($w_src, $h_src, $type) = getimagesize($file_src); // create new dimensions, keeping aspect ratio
$ratio = $w_src/$h_src;
if ($w_dst/$h_dst > $ratio) {$w_dst = floor($h_dst*$ratio);} else {$h_dst = floor($w_dst/$ratio);}
switch ($type){
case 1: // gif -> jpg
$img_src = imagecreatefromgif($file_src);
break;
case 2: // jpeg -> jpg
$img_src = imagecreatefromjpeg($file_src);
break;
case 3: // png -> jpg
$img_src = imagecreatefrompng($file_src);
break;
}
$img_dst = imagecreatetruecolor($w_dst, $h_dst); // resample
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $w_dst, $h_dst, $w_src, $h_src);
imagejpeg($img_dst, $new_img); // save new image
unlink($file_src); // clean up image storage
imagedestroy($img_src);
imagedestroy($img_dst);
}
$p_id = (Integer) $_POST[uid];
$ver = (Integer) $_POST[ver];
$delver = (Integer) $_POST[delver];
convertPic("your/file/structure/", 150, 150, "u".$p_id."v".$ver.".jpg", "u".$p_id."v".$delver.".jpg");
?>
“大圖片產生縮圖 導致imagecreatefromjpeg 記憶體崩潰問題”的更多相關文章 》
愛J2EE關注Java邁克爾傑克遜視頻站JSON線上工具
http://biancheng.dnbcw.info/php/330430.html pageNo:11