So there were a couple of problems with my last image processing functions (http://nodstrum.com/2006/12/09/image-manipulation-using-php/).
1. If you were trying to process a massive image > 1024px×768px it failed.
2. I only accounted for people processing Jpegs.
3. CPU spikes, because of the amount of processing that is being done with the image the CPU is really taking a hit.
4. The functions were really quite long.
These new function will use ImageMagicK, not all servers have ImageMagicK installed by default, check with your hosting provider.
So, here are the functions…
Thumbnail
// ImageMagicK doesnt like ‘/’ and ‘x’ characters in the command line call.
$uploadedImg = ”. $imgPath .‘/’. $img .”;
$newThumb = ”. $thumbDir .‘/’. $newName .”;
$newRes = ”. $newWidth .‘x’. $newHeight .”;
// This makes a command line call to ImageMagicK.
// My path to ImageMagicK Convert is ‘/usr/lib/php/bin/convert’
// ‘convert’ is a program (UNIX) so no forward slash.
$ct = system("/usr/lib/php/bin/convert -resize $newRes $uploadedImg $newThumb", $retval);
return $ct;
}
Resize
// ImageMagicK doesnt like ‘/’ and ‘x’ characters in the command line call.
// And workout the size based on ‘$by’.
$uploadedImg = ”. $imgPath .‘/’. $img .”;
$newResized = ”. $reduceDir .‘/’. $newName .”;
list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
$newWidth = ($width/$by);
$newHeight = ($height/$by);
$newRes = ”. $newWidth .‘x’. $newHeight .”;
// This makes a command line call to ImageMagicK.
// My path to ImageMagicK Convert is ‘/usr/lib/php/bin/convert’
// ‘convert’ is a program (UNIX) so no forward slash.
$cr = system("/usr/lib/php/bin/convert -resize $newRes $uploadedImg $newResized", $retval);
return $cr;
}
!! Important: you must ensure that when you pass $img to the functions that it has no spaces in it… here is an example (replacing spaced with ‘_’ underscores. Also dont forget to rename the actual file. !!
Here are some examples.
Building Original (916K (2576×1932))
Building Thumb (20K (120×90))
Building Resized and Reduced (71K (515×386))
8 Responses for "Image Manipulation using PHP and ImageMagicK"
PHP contains built-in functions for resizing images you don’t need to execute shell commands to do it.
Miro,
There is a reason for executing the shell commands, ImageMagicK is alot quicker than the GD ones built into PHP, it can also handle alot larger files.
Jamie.
There is an error in your createResizedIMK function:
$newResized = â€. $reduceDir .‘/’. $newName .â€;
$reduceDir should be $thumbDir.
or change the name in the parameters.
You should also show how to use your functions with imagemagick running on windows.
it’s me again, i forgot to mention that ImageMagick has thumbnail and resize functions and a lot more.
convert -thumbnail 100×100 img-from img-to
yep, one line.
Anywaz, keep up the good work!.
can ImageMagicK handle a 12MB jpeg file?
Hi Ian,
I do not see why not… if you try it can you let all of us here know?
Cheers,
Jamie.
Hi All,
I am still building my image magick functions to handle all kinds of image manipulation from standard resizing and cropping to creating polaroid effects. ImageMagick works great and has handled a 45Mb jpg file without a problem (except for the delay :-)). The only thing that gets me is that I don’t need to run it as a system command, locally or on the host. I also use passthrough so it doesn’t save new files and stuff.
Once I have the code I will try to remember to post it. It is fairly big as it handles a lot of error checking.
Great site, clean simple and straight to the point - you rock!
works like a charm. Found another tutorial on the imagemagick website that shows how to generate multiple images and put them together as a prgressive gif making look like a slide show. I’ll try and find the link to it again and post it here
Leave a reply