If you’ve ever done any sort of image processing in your PHP applications, you’ll start to realise how limited you are when using native PHP commands such as createimagefromjpg etc. It eats your web servers memory! Nowadays, what with people carrying 5 Megapixel cameras in their phones, uploading and resizing a photo can be areal strain on resources, especially if several users on the website are doing it at the same time.

To get around this conundrum, there is a PHP library called imagick which allows you to access a program called ImageMagick. ImageMagick runs natively on the machine, and is available for Unix, Linux, Mac, and Windows, so running it shouldn’t be a problem. The only thing to take into consideration here is whether your hosting providers PHP has imagick available. If not, depending on your hosting package you may be able to SSH into your server and install it.

ImageMagick can do all sorts of crazy image editing things, and can handle pretty much whatever you throw at it! Find out more here:
http://www.imagemagick.org/script/index.php

Anyway, looks good, so let’s get cracking! If you have been following my posts you’ll see I upgraded my PHP from the old version that ships with Lion using ports. I’m also going to use ports to get ImageMagick.

sudo port install ImageMagick

This will take up to ten minutes! It gets all its dependencies, configures, and compiles from the source code specifically for your computer’s setup. Once it’s done there are several commands you can run from the terminal:

animate
compare
composite
conjure
convert
display
identify
import
mogrify
montage
stream

For more info see http://www.imagemagick.org/script/command-line-tools.php

Now that we’ve done that, we need to install the imagick PHP library that accesses it.
Depending on your luck this may work straight away for you:

sudo pecl install imagick

When it asks for the prefix, type it /opt/local.

If it fails to install, you are probably running PHP 5.4 like me. In this case, you need to tell pear to use the latest beta, which WILL compile.

sudo pear config-set preferred_state beta
sudo pecl install imagick
sudo pear config-set preferred_state stable

Last but least you should add “extension=imagick.so” to php.ini, and restart apache. Boom! now we have a very powerful tool for image manipulation!

Update: If you get this error – Please provide a path to MagickWand-config or Wand-config program.
Then – ensure “ImageMagick-devel” and NOT “ImageMagick” package is installed instead

Advertisements