1

当我尝试裁剪图像时收到此错误,我刚刚上传。我刚刚上传的图像大约 3MB。

这是错误

<br />
<b>Fatal error</b>:  Out of memory (allocated 45088768) (tried to allocate 15552 bytes) <br />

当我尝试使用此功能时会发生这种情况: imagecreatefromjpeg($src);

我在顶部:

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

那么它怎么会内存不足,我应该如何解决这个问题?我的图片上传标准限制为 6MB。(虽然它可以很好地上传到服务器,但是当我使用裁剪时,我在使用 imagecreatefromjpeg() 函数时遇到错误,并且“内存不足..”)

4

3 回答 3

2

使用 GD进行图像处理时,请务必在之后释放已使用的内存imagedestroy并尽早执行。因此,当您不再需要图像资源时,请imagedestroy立即将其销毁。

于 2010-11-11T20:46:33.877 回答
1

PHP usually allows you to change the memory_limit setting during runtime. There are some extensions though, like Suhosin, that will disable this behaviour. Check to see if your PHP setup has the Suhosin extension loaded using phpinfo(). If so, check to see if the *suhosin.memory_limit* setting is set to 0. If so, that would explain this behaviour (not updating memory_limit) and you'll have to contact the sysadmin to get that limit raised.

Also see http://www.hardened-php.net/suhosin/configuration.html#suhosin.memory_limit

于 2010-11-11T20:41:34.200 回答
0

一个安全的解决方案是应用一些过滤器来阻止图像处理(或至少给出一些警告),当进程需要比允许的限制更多的内存时。imgcreatefromjpeg上的 PHP 手册有一些用户功能(检查评论),可以计算处理图像所需的内存。根据计算,您可以限制上传图片的最大尺寸,以防止用户触发内存限制错误。

注意:不要忘记在计算中增加几 MB。

但是,如果您坚持不限制图像大小,请尝试要求您的主机增加您的内存限制或使用第三方服务,如Google Images Python Api

于 2010-11-11T22:30:08.267 回答