PHP GD 允许的内存大小已用完
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2827908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP GD Allowed memory size exhausted
提问by gurun8
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 exhaustedPHP Fatal error.
我正在尝试使用 PHP: GD 处理一个 JPEG 图像目录(大约 600 多个,范围从 50k 到 500k)来调整图像大小并保存图像,但在此过程的早期我遇到了一些障碍。仅正确处理 3 个图像(30K、18K 和 231K)后,我得到16777216 字节的Allowed memory size of 16777216 字节耗尽PHP 致命错误。
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 imagedestroyfunction but it doesn't seem to have any affect. The script just keeps consistently choking at the imagecreatefromjpegline of code.
我试图用该imagedestroy函数释放内存,但它似乎没有任何影响。该脚本一直在imagecreatefromjpeg代码行处窒息。
I checked the php.ini and the memory_limit = 16Msetting 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.
我检查了 php.ini,memory_limit = 16M设置似乎正确。但是我不知道为什么内存被填满了。它不应该将内存释放回垃圾收集器吗?我真的不想增加 memory_limit 设置。这似乎是一个糟糕的解决方法,将来可能会导致更多问题。
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?
谁能看到我是否只是遗漏了一些简单的东西,或者这里是否存在设计缺陷?您会认为这是一项非常简单的任务。这肯定是可能的,对吧?
回答by TravisO
ini_set('memory_limit', '64M');
problem solved
问题解决了
回答by JYelton
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.
您的一个或多个图像可能在原始内存中实际膨胀到 16M。一种检查方法是在 Photoshop 或 Irfanview 中打开它并检查色彩空间和像素尺寸。
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:
达到 16M 并不需要太多,例如,考虑一个“低”的 6 兆像素相机。它创建了一个 3072 x 2048 像素的图像。以每色字节 (RGB) 计算,原始大小为:
3072 x 2048 x 3 = 18,874,368
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.
因此,您可能希望根据您希望处理的最大图像来增加内存。但是您必须考虑它们的原始大小。
回答by JM Volpeliere
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.
并将处理崩溃的代码放在函数“my_function()”中。
回答by Mark Lalor
Simply use ini_set();and set the memory_limit to whatever size you want.
只需使用ini_set();并将 memory_limit 设置为您想要的任何大小。

