图像处理-PHP脚本GD库内存问题


image processing - PHP script GD library memory problem

在我的网站上,我允许用户上传6张图像,最大大小为5MB。它们的格式必须是gif、jpg/jpeg。用户上传的大多数图像大约为2MB(1006188字节),高度:1536像素,宽度:2048像素,非常大(尤其是在尝试调整大小的情况下)。

然后,我在服务器上有两个目录(large_img和small_img)。在large_img中,我将上传的文件移到那里。然后,我使用GD库将图像从large_img目录重新调整为缩略图大小(100高乘100宽),并将其移动到small_img中。

我偶尔会出现错误

PHP Fatal error:  Out of memory (allocated 80740352) (tried to allocate 14592 bytes)

我的apache服务器上允许的最大内存是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'>&#8226; 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'>&#8226; 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照片格式

我正在通过破坏图像$imagedestroy($newsmallerimage); 来释放内存

请注意,对于刚刚命名为['ploadedfile_1']的其他5个图像,重复使用相同的代码if()语句。。。2等

任何建议都可能使用file_get_contents或GD库将文件读取到内存中。我知道GD将未压缩的整个图像保存在内存中,这可能是个问题。谢谢各位

您没有释放$im,也许这就是问题所在。

销毁您通过imagecreatefromjpeg() 创建的图像

if()条件下添加行,

imagedestroy($im);

像这样,

if(!$im)
{
    $image_resize_error++; 
    $array_error_msg_resize[$image_resize_error]="<p class='form_error_messages'>&#8226; An unfixable error occurred with your image ('<span class='standard_span'> " .$_FILES['uploadedfile_0']['name']." </span>').</p>";
    imagedestroy($im);
}