PHP GD允许的内存大小用尽 [英] PHP GD Allowed memory size exhausted
问题描述
我正在尝试使用PHP:GD处理JPEG图像目录(大约600+,范围从50k到500k),以调整图像大小并保存图像,但是在此过程的早期我遇到了一些麻烦.正确处理了3张图像(30K,18K和231K)后,我得到了已用完的16777216字节内存大小 PHP致命错误.
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);
我尝试使用imagedestroy
函数释放内存,但似乎没有任何影响.该脚本只是始终使imagecreatefromjpeg
代码行令人窒息.
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.
我检查了php.ini,并且memory_limit = 16M
设置似乎正确保存了.但我不知道为什么内存已满.它不应该将内存释放回垃圾回收器吗?我真的不想增加memory_limit设置.这似乎是一个错误的解决方法,将来可能会导致更多问题.
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?
推荐答案
ini_set('memory_limit', '64M');
问题解决了
这篇关于PHP GD允许的内存大小用尽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!