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))
13 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
Hello, I’m also having problems executing “convert” using PHP.
First the path was wrong, and the system command gave me a return value of 127. Then I fixed the path to the convert command (/Applications/ImageMagick-6.4.4/bin/convert), and I fixed the paths to the input and output filenames. Here is the code I’m using:
<?php
$resizefile = “/Applications/ImageMagick-6.4.4/bin/convert /Users/shafiquejamal/outsideofwebdir/uploads/IMG_0041.JPG /Users/shafiquejamal/outsideofwebdir/uploads/41.png”;
echo $resizefile;
$last_line = system($resizefile , $retval);
// Printing additional info
echo ‘
Last line of the output: ‘ . $last_line . ‘
Return value: ‘ . $retval;
?>
The return value is 5, when I execute this; it does not convert the file. BUT When I copy and past the resize command exactly (without quotes) into the terminal window, it works just fine - it converts the file. It just doesn’t work from php (i.e. the php script executed from the brower does not do anything). I’m running MAC OS X Leopard 10.5.5 on a MacBook Pro (Intel), and using ImageMagick-6.4.4 I’ve changed permissions on the source and target folders using chmod a+rw {paths}, and checked that everyone can read and write to those directories.
shafique: you may want to check the access rights & permisions you have for PHP vs terminal user
Hello HW,
Thanks for the suggestion about the access rights. The PHP user operates as “_www”. Changing permissions recursively on the IM directory to give all users rwx permissions, which I did, should have taken care of any permissions issues. I ran some tests using php script to see what kind of errors were being returned, and here’s what I came up with:
Here’s the testing code:
$array=array();
echo “”;
exec(”/Applications/ImageMagick-6.4.4/bin/convert IMG_0041.JPG 41.png 2>&1″, $array);
echo “”.print_r($array).”";
echo “”;
and here’s the error that the above script generated:
Array
(
[0] => dyld: Library not loaded: /ImageMagick-6.4.4/lib/libMagickCore.1.dylib
[1] => Referenced from: /Applications/ImageMagick-6.4.4/bin/convert
[2] => Reason: image not found
)
What’s odd is that running the following command from the command line in a terminal window:
/Applications/ImageMagick-6.4.4/bin/convert IMG_0041.JPG 41.png 2>&1
works just fine. I’m thinking to focus on the first error:
[0] => dyld: Library not loaded: /ImageMagick-6.4.4/lib/libMagickCore.1.dylib
It seems that as a terminal user the library loads, but as “_www” it does not. Since the permissions are fine, maybe the paths need to be changed for the “_www” Are path settings different for different users? Your thoughts?
SOLVED!!!!!!
See the following link:
http://www.daniweb.com/forums/thread116143.html
And here’s my solution (I uninstalled IM and reinstalled it into my home directory, that’s why the paths are different from what I listed before. But this has nothing to do with what the problem was. I also changed the user from www to www2 - I added the www2 user and made a change in the httpd.conf file - but again this was part of troubleshooting and had nothing to do with the problem):
Code:
putenv(”MAGICK_HOME=” .$_ENV["MAGICK_HOME"]. “/Users/shafique/ImageMagick-6.4.5/”);
exec(’echo $MAGICK_HOME’,$array);
putenv(”PATH=” .$_ENV["PATH"]. “/Users/shafique/ImageMagick-6.4.5/bin”);
exec(’echo $PATH’,$array);
putenv(”DYLD_LIBRARY_PATH=” .$_ENV["DYLD_LIBRARY_PATH"]. “/Users/shafique/ImageMagick-6.4.5/lib”);
exec(’echo $DYLD_LIBRARY_PATH’,$array);
echo “”.print_r($array).”";
exec(’/Users/shafique/ImageMagick-6.4.5/bin/convert /Users/www2/IMG_0041.JPG /Users/www2/41.png 2>&1′, $array);
So the problem was that my environment variables would not stay set they way that I had set them after installing IM; I have to set them each time I run the php code, as above. Note that simply using:
Code:
exec( ‘export $DYLD_LIBRARY_PATH=”/Users/shafique/ImageMagick-6.4.5/lib” ‘,$array);
(as per the IM installation instructions) will NOT work; the environment variable will NOT stay set this way (at least on my Leopard 10.5 MacBook Pro). Note also it necessary that both the input and output files be in a directory that is accessible to all (I did chmod a+rw /Users/www2).
I hope this helps others!
Wow! Thank you very much. I had exactly the same problem. Your solution saved me a lot of time
Leave a reply