我正在对最大 10MB 的文件使用单个图像上传/裁剪/调整大小脚本。
测试时,我将 php_ini 内存限制设置为 25M,上传文件仅约 1.4MB 时已耗尽。
"Allowed memory size of 26214400 bytes exhausted (tried to allocate 10368 bytes)"
这对我来说似乎很奇怪,不是 10368 < 26214400 吗?(反问)
或者这是否意味着我在 25MB 上占用了 10368 字节?我的脚本应该使用这么多内存吗?
代码:
function make_thumbnails($updir, $img)
{
$thumbnail_width = 200;
$thumbnail_height = 150;
$thumb_preword = "thumb_";
$arr_image_details = GetImageSize($updir.$img);
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];
if( $original_width > $original_height ){
$new_width = $thumbnail_width;
$new_height = intval($original_height*$new_width/$original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width*$new_height/$original_height);
}
$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);
if($arr_image_details[2]==1) { $imgt = "ImageGIF"; $imgcreatefrom = "ImageCreateFromGIF"; }
if($arr_image_details[2]==2) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG"; }
if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG"; }
if( $imgt )
{
$old_image = $imgcreatefrom($updir.$img);
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imageCopyResized($new_image,$old_image,$dest_x,
$dest_y,0,0,$new_width,$new_height,$original_width,$original_height);
$imgt($new_image,$updir.$thumb_preword.$img);
// Added by your suggestions:
imagedestroy($old_image);
imagedestroy($new_image);
}
}