I’m coding a little CMS, where you can upload several images.
These images are converted into 3 versions (big, mid und thumbnail size) with imagemagick.
The problem is that imagemagick needs 5 minutes for creating these 3 versions of 4 pictures (which were uploaded).
here is the part with the imagemagick commands:
foreach($upIMGS as $key => $filename){
list($width, $height) = getimagesize($path.$filename);
if ($width > $height) $size = "x96";
else $size = "96x";
exec(P_IMAGEMAGICK." ".$path.$filename." -resize $size -gravity center -crop 96x96+0+0 +repage ".$path."th-".$filename);
exec(P_IMAGEMAGICK." ".$path.$filename." -resize 320x320 ".$path."hl-".$filename);
exec(P_IMAGEMAGICK." ".$path.$filename." -resize 514x ".$path."fl-".$filename);
unlink($path.$filename);
}
[the $upIMGS is an array with all the filenames of the recently uploaded images]
I mean.. it does work, but toooo slow and after 5min the server gives me an error. some of the files are generated and some are not…
Would be very nice if you can give me a tip.
I recently ran into the same issue, but I was only passing through the images once to resize them from their original 2592×1944 to 300xbestFit or bestFitx300
I am using the PHP class imagick instead of command lines but I cut my time in half by changing to -scale or scaleImage in my case. Here is a snippet of my test code.
I am processing 29 images at 2592×1944 and went from 23.3927891254 seconds to 11.555036068 seconds. I hope this helps.
Edit:
In addition to what I said above I just ran into to the following, which might be helpful, on ImageMagick v6 Examples — API & Scripting:
“Shell scripts are inherently slow. It is interpreted, require
multiple steps and extra file handling to disk. This is of course
better thanks to the new IM v6 option handling, allowing you to do a
large number of image processing operations in a single command. Even
so you can rarely can do everything in a single
convertcommand, soyou often have to use multiple commands to achieve what you want.”
“When reading large or even a large number of images, it is better to
use a Read
Modifier to
resize, or crop them, as IM does not them read in the full image,
thus reducing its memory requirement.”
“If you call ImageMagick as an Apache module it will also reduce
startup time, as parts will be loaded once and kept available for
multiple use, rather than needing re-loading over and over. This may
become more practical in the future with a permanently running
‘daemon’ IM process.”