I got stuck for days (and nights) with this problem.
Moreover, providers memory limit differs from one to another.
And change the memory_limit
in PHP does not work on shared servers, providers usually limit the ram even if phpinfo()
says that you got 128Mo (1and1 limits RAM to 60Mo per process for example).
But I finally found something quite efficient here : http://www.imagemagick.org/Usage/files/#massive
It needs imagemagick
, but I discovered that most of the providers natively provides this on their servers, even shared ones.
exec('env MAGICK_TMPDIR=<tmp_dir> nice -5 convert -limit memory 32 -limit map 32 -resize 800x600 huge.jpg reasonable.jpg');
As it's said :
env MAGICK_TMPDIR=<tmp_dir>
set up a temp directory for imagemagick
to simulate ram (kind of)
nice -5
is also a unix command to change the priority of a process
(http://en.wikipedia.org/wiki/Nice_(Unix))
convert ...
is the imagemagick command line
The real deal is about -limit memory 32
and -limit map 32
. This is the way you limit the memory used by the binary (here : 32Mo). You will probably need to fit the values to match the values of your server (Generally PHP tells you the maximum allocated memory when it gives you the Fatal Error. I suggest you to divide this value by 2 or 4 to be confortable).
I also needed to put some other lines in my PHP to avoid some collateral issues :
ignore_user_abort(true); // ignore user abort : let the script finish resizing even if user aborts
set_time_limit(0); // ignore server timeout
putenv('MAGICK_THREAD_LIMIT=1'); // limit the number of thread for the binary. Very important in my case
Hope all that will help ...
To know if convert
is available on your server, you can try this (in PHP) :
$out = array();
exec('which convert 2>&1', $out);
print_r($out);
That will gives you the path of the binary, if exists.