在我的网站上,我允许用户上传6张图片,最大大小为5MB。它们必须格式为gif,jpg/jpeg。用户似乎上传的大多数图片大约为2MB(1006188字节),高度为1536像素,宽度为2048像素,因此非常大(特别是在尝试调整大小时)。PHP脚本GD库内存问题
然后我在服务器上有两个目录(large_img和small_img)。在large_img中,我将上传的文件移动到那里。然后,我使用GD库将该图像从该large_img目录重新调整为缩略图大小(100宽×100),然后将其移至small_img。
我已经越来越错误让我的Apache服务器上偶尔
PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 14592 bytes)
最大内存为64MB。因此,我的代码存在一些问题,例如,使用这个64MB的几个块,并且出于某种原因不释放它。
这是我的代码来拍摄图像并调整它的大小。我已经提供它出6的第一张图像,并没有包括在这里以多错误检查,复制和粘贴:
if(move_uploaded_file($_FILES['uploadedfile_0']['tmp_name'], $move_file_to_directoryname_large))
{
$n_width=100;$n_height=100; /*specify height/width of thumbnail image to be*/
if($_FILES["uploadedfile_0"]["type"]=="image/gif")
{
$im=imagecreatefromgif($move_file_to_directoryname_large);
if(!$im)
{
/*error could occur here if image was say named .gif but was another type like .jpg casing file type error*/
$image_resize_error++;
$array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>• An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>";
}
else
{
$width=imagesx($im); $height=imagesy($im);
$newsmallerimage=imagecreatetruecolor($n_width,$n_height);
imagecopyresized($newsmallerimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
$move_file_to_directoryname_small=$target_path_small.$newfilename;
if(function_exists("imagegif"))
{
imagegif($newsmallerimage,$move_file_to_directoryname_small);
$images_db_small[0]=substr_replace($move_file_to_directoryname_small, "", 0, 24);
}
/*frees image from memory */
imagedestroy($newsmallerimage);
}
}
if($_FILES["uploadedfile_0"]["type"]=="image/jpeg")
{
$im=imagecreatefromjpeg($move_file_to_directoryname_large); /*create from image stored in directory specified when moving from /tmp*/
if(!$im)
{
/*error could occur here if image was say named .gif but was another type like .jpg casing file type error*/
$image_resize_error++;
$array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>• An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>";
}
else
{
$width=imagesx($im);/*Original picture width is stored*/$height=imagesy($im);/*Original picture height is stored*/
$newsmallerimage=imagecreatetruecolor($n_width,$n_height);
$imagecopyresized($newsmallerimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
$move_file_to_directoryname_small=$target_path_small.$newfilename;
if(function_exists("imagejpeg"))
{
imagejpeg($newsmallerimage,$move_file_to_directoryname_small);
$images_db_small[0]=substr_replace($move_file_to_directoryname_small, "", 0, 24);
}
/*frees image from memory */
imagedestroy($newsmallerimage);
}
}
}
继承人我的php.ini设置:
upload_max_filesize = 30M
post_max_size = 30M
max_execution_time = 120
max_file_uploads = 6
memory_limit=128M
在东西剧本正在吃掉记忆。发生上述错误的代码$im=imagecreatefromjpeg($move_file_to_directoryname_large);
这部分时,如果它的JPEG照片或imagecreatefromgif()如果GIF图片格式
我被if()语句是破坏形象$imagedestroy($newsmallerimage);
注意:此相同的代码释放内存对于刚刚命名为['uploadedfile_1']的其他5张图像重复... 2等
任何建议可能使用file_get_contents或GD库将文件读入内存。我知道GD将整个图像保存在内存中,这可能是问题所在。 谢谢你们
你'任何理由function_exists( 'imagejpeg')' ?如果GD没有安装,或者没有启用jpeg支持,你的脚本就会死在'imagecreatefromjpeg()'。 – 2011-06-15 18:25:04
@Marc乙 - 没有任何理由,我不需要,如果(function_exists())部分可以摆脱那个不能,我把它的代码里面 – daza166 2011-06-15 18:37:02