【问题标题】:PHP script GD library memory problemPHP脚本GD库内存问题
【发布时间】:2011-09-15 18:18:04
【问题描述】:

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

然后我在服务器上有两个目录(large_img 和 small_img)。在 large_img 中,我将上传的文件移到那里。然后,我使用 GD 库将来自该 large_img 目录的图像重新调整为缩略图大小(100 高 x 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);if its jpeg photo or imagecreatefromgif() if its gif photo format

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

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

任何建议都可能使用 file_get_contents 或 GD 库将文件读入内存。我知道 GD 将整个图像未压缩地保存在内存中,这可能是问题所在。 谢谢大家

【问题讨论】:

  • 你有什么理由这样做function_exists('imagejpeg')?如果未安装 GD 或未启用 jpeg 支持,您的脚本将在 imagecreatefromjpeg() 处死掉。
  • @Marc B - 没有任何理由我不需要 if(function_exists()) 部分可以摆脱那个不能我并将其中的代码带到外面

标签: php image-processing file-upload imagecreatefrompng


【解决方案1】:

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

【讨论】:

  • 那么我该如何释放它,imagedestroy() 不这样做吗?
  • @daza:你只是破坏了调整大小的图像。 $im 是原始图像,您不要对其进行破坏。
  • 发现我没有破坏图像($im)。只有 imagedestroy($newsmallerimage) 难怪我内存不足,这就是错误日志所指向的。谢谢
【解决方案2】:

通过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);
}

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-17
    • 2010-11-16
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    相关资源
    最近更新 更多