【问题标题】:Allowed memory size of 134217728 bytes exhausted (tried to allocate 18472 bytes)允许的内存大小 134217728 字节用尽(尝试分配 18472 字节)
【发布时间】:2014-11-20 04:22:51
【问题描述】:

我在这一行收到上述错误:

imagefill($canvas, 0, 0, imagecolorallocate($canvas, 255, 255, 255));

该脚本适用于所有图像类型,但它会在分辨率大于 4000 x 3000 像素的图像上崩溃。图像本身只有 500kb。 这是所有代码:

$canvas = imagecreatetruecolor(imagesx($source), imagesy($source));
imagefill($canvas, 0, 0, imagecolorallocate($canvas, 255, 255, 255));
imagealphablending($canvas, TRUE);
imagecopy($canvas, $source, 0, 0, 0, 0, imagesx($source), imagesy($source));
imagedestroy($source);
$quality = 100; // 0 = worst / smaller file, 100 = better / bigger file 
imagejpeg($canvas, $strPath.$p_Id.".jpg", $quality);        //----- save image
ImageDestroy($canvas);

【问题讨论】:

  • 4000 像素 x 3000 像素 @ 4 字节/像素 = 48MB 只是为了将该图像保存在内存中,甚至在您开始操作它之前,并且您正在复制图像,因此您拥有该 48MB 的多个副本在记忆中......你达到极限并不奇怪
  • 500KB 只是应用有损压缩后的文件大小。如果你需要操作图片,你必须在内存中分配它们。

标签: php memory


【解决方案1】:

无论压缩图像的大小是多少,您的库似乎都需要更多内存。我看到你正在使用 128 mb atm,通过使用将其增加到 256:

ini_set('memory_limit','256M');

【讨论】:

  • 脚本顶部没问题。
  • 当然。你应该接受我的回答,所以人们停止回复:)
【解决方案2】:

我将覆盖 PHP 中的默认内存限制:

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

【讨论】:

    【解决方案3】:

    无论压缩图像的大小是多少,您的库似乎都需要更多内存。我看到你正在使用 128 mb atm,将其增加到最多

    ini_set('memory_limit', 0);
    

    【讨论】:

    • PHP 文档说,它需要 -1,而不是零。这个答案已经由derdida提供了
    猜你喜欢
    • 2016-04-18
    • 2013-08-09
    • 2021-12-29
    • 2017-07-23
    • 2018-02-23
    • 2017-10-30
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    相关资源
    最近更新 更多