I run a small VPS for one of our sites and am using imagemagick to resize images with imagefield and imagecache.

If someone uploads a large image then imagemagick resizes it to the max dimensions given by imagefield. However, if it is a seriously large image this puts strain on the server, in fact with the limited RAM and /tmp on my VPS imagemagick has crashed my server doing this.

Imagemagick itself is outside PHP so it doesn't get limited by the php.ini memory limit. The solution seems to be using something like "-limit memory 64 -limit map 128" with the imagemagick convert command. And maybe use "nice convert..." too. But I don't know how to do that as the command is happening through the imageAPI module.

Is there a way I can set the maximum memory usage for imagemagick? Where is the "convert" command being called in the module, that I might add these switches to it?


ice5nake’s picture

I could benefit from the answer to this question as well.

ice5nake’s picture

Category: support » feature
Priority: Normal » Major
Issue tags: +convert, +imagemagick, +server crash

The convert program is called in _imageapi_imagemagick_convert_exec.

Which is called by _imageapi_imagemagick_convert. _imageapi_imagemagick_convert seems to be where command line arguments get set. Currently only the quality option is being used.

I wonder what version of ImageMagick introduced -limit. The -limit option is described here: http://www.imagemagick.org/script/command-line-options.php#limit

I changed this to a feature request and changed the priority to major as at least two people have had server crashes related to this.

Alternatively, I wonder if there is a way to alias the convert program for the Apache user so that anytime Apache calls convert it is called with the limits in place.

ice5nake’s picture

I created the following bash script.

I made its owner apache and made it executable. I then set the convert path setting in all the image modules to use the path to the shell script instead of converts actual path.

#Uncomment next echo to have logging 
#echo "$@" >> /var/www/convert.log
nice -n 19 /usr/bin/convert -limit memory 64 -limit map 128 $@
exit $?