图像magick/PHP掉落了大图像 [英] Image magick/PHP is falling over with large images

查看:69
本文介绍了图像magick/PHP掉落了大图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,用于调整用户FTP文件夹中图像的大小,以便在其网站上使用.

I have a PHP script which is used to resize images in a user's FTP folder for use on his website.

虽然调整大小的速度很慢,但是该脚本已正确完成了过去的所有图像.但是最近,该用户上传了一个21Megapixel JPEG图像的相册,并且正如我所发现的,脚本无法转换图像,但没有给出任何PHP错误.当查阅各种日志时,我发现多个Apache进程因内存不足错误而被杀死.

While slow to resize, the script has completed correctly with all images in the past. Recently however, the user uploaded an album of 21-Megapixel JPEG images and as I have found, the script is failing to convert the images but not giving out any PHP errors. When I consulted various logs, I've found multiple Apache processes being killed off with Out Of Memory errors.

PHP脚本的功能部分实质上是一个for循环,该循环遍历磁盘上的图像并调用一种方法,该方法检查是否存在缩略图,然后执行以下操作:

The functional part of the PHP script is essentially a for loop that iterates through my images on the disk and calls a method that checks if a thumbnail exists and then performs the following:

$image = new Imagick();
$image->readImage($target);
$image->thumbnailImage(1000, 0);
$image->writeImage(realpath($basedir)."/".rescale."/".$filename);
$image->clear();
$image->destroy();  

服务器具有512MB的RAM,通常至少有360MB +的可用空间.

The server has 512MB of RAM, with usually at least 360MB+ free.

PHP当前将其内存限制设置为96MB,但我之前将其设置为更高,对此问题没有任何影响.
据我估计,一个21兆像素的图像在未压缩时应该占据80MB +的区域,因此,我对为什么RAM如此迅速消失的问题感到困惑,除非不从内存中删除Image Magick对象.

PHP has it's memory limit set currently at 96MB, but I have set it higher before without any effect on the issue.
By my estimates, a 21-Megapixel image should occupy in the region of 80MB+ when uncompressed, and so I am puzzled as to why the RAM is disappearing so rapidly unless the Image Magick objects are not being removed from memory.

是否可以通过某种方式优化脚本以更有效地使用更少的内存或垃圾回收?
我是否没有足够的RAM来应付如此大的图像?

Is there some way I can optimise my script to use less memory or garbage collect more efficiently?
Do I simply not have the RAM to cope with such large images?

欢呼

推荐答案

$image->readImage()之前调用$image->setSize()以使libjpeg在加载时调整图像大小,以减少内存使用.

Call $image->setSize() before $image->readImage() to have libjpeg resize the image whilst loading to reduce memory usage.

(编辑),示例用法:使用PHP高效调整JPEG图像大小

这篇关于图像magick/PHP掉落了大图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆