gpt4 book ai didi

PHP GD 允许内存大小耗尽

转载 作者:可可西里 更新时间:2023-10-31 22:18:47 24 4
gpt4 key购买 nike

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

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

    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 设置似乎正确。但我不明白为什么内存被填满了。它不应该将内存释放回垃圾收集器吗?我真的不想增加 memory_limit 设置。这似乎是一个糟糕的解决方法,可能会在未来导致更多问题。

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

任何人都可以看看我是否只是遗漏了一些简单的东西,或者这里是否存在设计缺陷?您会认为这是一项非常简单的任务。这肯定是可能的,对吧?

最佳答案

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

问题解决了

关于PHP GD 允许内存大小耗尽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2827908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com