【问题标题】:PHP Image Resize Fatal error: Out of memoryPHP Image Resize 致命错误:内存不足
【发布时间】:2019-03-18 15:26:14
【问题描述】:

我有以下 PHP 代码可以将我的图片调整为所需的大小:

/* POSTER - resize */
                $remote_file = $castImages[$name];
                $new_width  = 296;
                $new_height = 436;
                list($width, $height) = getimagesize($remote_file);
                $image_p = imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefromjpeg($remote_file);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                imagejpeg($image_p, '../glumci/'.$name.' BIG.jpg', 100);
                $new_width  = 74;
                $new_height = 109;
                list($width, $height) = getimagesize($remote_file);
                $image_p = imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefromjpeg($remote_file);
                imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                imagejpeg($image_p, '../glumci/'.$name.'.jpg', 100);
                imagedestroy($image_p);
                imagedestroy($image);

我有大约 5500 张图片需要调整大小,所以我将这段代码运行到 while 循环中并从 PHP 中得到这个错误:

Fatal error: Out of memory (allocated 473956352) (tried to allocate 27263000 bytes) in D:\portal_ONLINE\UwAmp\www\inc\test.php on line 54

然后我在 PHP 脚本中添加这段代码:

ini_set('memory_limit', '-1');

但我收到相同的错误消息..那么如何修复此错误,以便脚本重命名所有 5500 张图片而不仅仅是 50 并抛出此错误?

【问题讨论】:

  • 试一试:unset($image_p); & unset($image);
  • 好的..我添加了代码..运行脚本,现在我们将查看..我将在大约 5 分钟后重播,看看脚本现在是否正常工作
  • 同样的错误...内存不足...你知道吗?
  • 试试$image_p = null, $image = null,当你使用unset时,只有垃圾收集器决定时才会释放内存
  • 我看不到您将图像写回文件的位置。您可能没有在处理该问题的任何函数中关闭文件吗?

标签: php memory out


【解决方案1】:

在上述重播答案的成员的帮助下,我得到了最终的工作代码:

/* POSTER - resize */
            $remote_file = $castImages[$name];
            $new_width  = 296;
            $new_height = 436;
            list($width, $height) = getimagesize($remote_file);
            $image_p = imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefromjpeg($remote_file);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            imagejpeg($image_p, '../glumci/'.$name.' BIG.jpg', 100);
            $image_p = null;
            $image = null;

            $new_width  = 74;
            $new_height = 109;
            list($width, $height) = getimagesize($remote_file);
            $image_p = imagecreatetruecolor($new_width, $new_height);
            $image = imagecreatefromjpeg($remote_file);
            imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            imagejpeg($image_p, '../glumci/'.$name.'.jpg', 100);
            imagedestroy($image_p);
            imagedestroy($image);
            $image_p = null;
            $image = null;

使用:

$image_p = null;
$image = null;

它现在可以运行大约 20 分钟,并且脚本正在运行并且没有抛出错误消息。

【讨论】:

    猜你喜欢
    • 2014-06-01
    • 1970-01-01
    • 2014-01-25
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    相关资源
    最近更新 更多