PHP GD 允许的内存大小已耗尽

发布于 2024-09-01 01:55:42 字数 1284 浏览 2 评论 0原文

我正在尝试使用 PHP:GD 处理 JPEG 图像目录(大约 600+,范围从 50k 到 500k)来调整图像大小并保存图像,但我在这个过程的早期就遇到了一些障碍。在正确处理 3 个图像(30K、18K 和 231K)后,我收到 Allowed memory size of 16777216 bytes exeded PHP Fatal 错误。

我正在循环浏览图像并调用下面的代码:

    list($w, $h) = getimagesize($src);

    if ($w > $it->width) {
        $newwidth = $it->width;
        $newheight = round(($newwidth * $h) / $w);
    } elseif ($w > $it->height) {
        $newheight = $it->height;
        $newwidth = round(($newheight * $w) / $h);
    } else {
        $newwidth = $w;
        $newheight = $h;
    }

    // create resize image
    $img = imagecreatetruecolor($newwidth, $newheight);
    $org = imagecreatefromjpeg($src);

    // Resize
    imagecopyresized($img, $org, 0, 0, 0, 0, $newwidth, $newheight, $w, $h);
    imagedestroy($org);

    imagejpeg($img, $dest);

    // Free up memory
    imagedestroy($img);

我尝试使用 imagedestroy 函数释放内存,但它似乎没有任何影响。该脚本始终在 imagecreatefromjpeg 代码行处令人窒息。

我检查了 php.ini,memory_limit = 16M 设置似乎保持正确。但我不明白为什么内存会被填满。难道不应该将内存释放回垃圾收集器吗?我真的不想增加内存限制设置。这似乎是一个糟糕的解决方法,可能会在未来导致更多问题。

仅供参考:我正在从命令提示符运行我的脚本。它不应该影响功能,但可能会影响您的响应,所以我认为我应该提及它。

谁能看出我是否只是错过了一些简单的东西或者这里是否存在设计缺陷?您可能认为这将是一项非常简单的任务。这当然是可能的,对吧?

I'm trying to process a directory of JPEG images (roughly 600+, ranging from 50k to 500k) using PHP: GD to resize and save the images but I've hit a bit of a snag quite early in the process. After correctly processing just 3 images (30K, 18K and 231K) I get a Allowed memory size of 16777216 bytes exhausted PHP Fatal error.

I'm cycling through the images and calling the code below:

    list($w, $h) = getimagesize($src);

    if ($w > $it->width) {
        $newwidth = $it->width;
        $newheight = round(($newwidth * $h) / $w);
    } elseif ($w > $it->height) {
        $newheight = $it->height;
        $newwidth = round(($newheight * $w) / $h);
    } else {
        $newwidth = $w;
        $newheight = $h;
    }

    // create resize image
    $img = imagecreatetruecolor($newwidth, $newheight);
    $org = imagecreatefromjpeg($src);

    // Resize
    imagecopyresized($img, $org, 0, 0, 0, 0, $newwidth, $newheight, $w, $h);
    imagedestroy($org);

    imagejpeg($img, $dest);

    // Free up memory
    imagedestroy($img);

I've tried to free up memory with the imagedestroy function but it doesn't seem to have any affect. The script just keeps consistently choking at the imagecreatefromjpeg line of code.

I checked the php.ini and the memory_limit = 16M setting seems like it's holding correctly. But I can't figure out why the memory is filling up. Shouldn't it be releasing the memory back to the garbage collector? I don't really want to increase the memory_limit setting. This seems like a bad workaround that could potentially lead to more issues in the future.

FYI: I'm running my script from a command prompt. It shouldn't affect the functionality but might influence your response so I thought I should mention it.

Can anyone see if I'm just missing something simple or if there's a design flaw here? You'd think that this would be a pretty straightforward task. Surely this has to be possible, right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

允世 2024-09-08 01:55:42
ini_set('memory_limit', '64M');

问题解决了

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

problem solved

说谎友 2024-09-08 01:55:42

您的一张或多张图像的原始内存实际上可能会膨胀到 16M。一种检查方法是在 Photoshop 或 Irfanview 中打开它并检查色彩空间和像素尺寸。

达到 16M 并不需要太多,例如,考虑一个“低级”6 兆像素相机。它创建一个 3072 像素 x 2048 像素的图像。按每种颜色 (RGB) 字节计算,原始大小为:

3072 x 2048 x 3 = 18,874,368

因此,您可能需要根据您期望处理的最大图像来增加内存。但你必须考虑它们的原始大小。

It's possible that one or more of your images actually inflate to 16M in raw memory. One way to check is to open it in Photoshop or Irfanview and check the color space and pixel dimensions.

It doesn't take much to reach 16M, for example, consider a "lowly" 6 megapixel camera. It creates a 3072 pixel by 2048 pixel image. At a byte per color (RGB) the raw size is:

3072 x 2048 x 3 = 18,874,368

So, you might want to increase the memory according to the largest images you expect to process. But you have to consider their raw size.

八巷 2024-09-08 01:55:42

在某些情况下,您根本无法预测要处理的图像所需的最高内存分配。为了防止崩溃,您可以在循环之前和之后包含以下命令:

register_shutdown_function ('my_function');
$mem_limit = ini_get ('memory_limit');
ini_set ('display_errors', false);
ini_set ('memory_limit', '400M');          // some high value

(...您的进程...)

ini_set ('memory_limit',$mem_limit);

并将处理崩溃的代码放入函数“my_function ()”中。

In some cases you simply cannot anticipate the highest memory allocation that will be needed by the images you are about to process. To prevent a crash, you may include following commands before and after your loop :

register_shutdown_function ('my_function');
$mem_limit = ini_get ('memory_limit');
ini_set ('display_errors', false);
ini_set ('memory_limit', '400M');          // some high value

(...your process...)

ini_set ('memory_limit',$mem_limit);

And place within the function "my_function ()" the code that will handle the crash.

原野 2024-09-08 01:55:42

只需使用 ini_set(); 并将 memory_limit 设置为您想要的任何大小。

Simply use ini_set(); and set the memory_limit to whatever size you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文