Sjoerd Maessen blog

PHP and webdevelopment

ImageMagick installing and exploring it, a good alternative for the GD library

without comments


Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/private_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /home/smaessen/domains/sjoerdmaessen.nl/private_html/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2147

Although PHP GD library handles basic image operations quite good, you have probably run into the limitations of GD library as well, it is often slow, memory intensive and a bit complicated to use for advanced image effects. ImageMagick however is a very good alternative. It is an opensource software suite licensed under a BSD style license from IM itself. ImageMagick provides in all functionality that can be accomplished with the GD library but even goes a step further with support for more advanced image edit techniques. An example of such a technique is the recently added “liquid scaling” feature, this is a content aware scaling option.

Click the image below to see an example of liquid scaling:
Liquid Scaling with imagemagick

So how do I get such a result? Well with PHP GD library it would take a whole lot of code, thinking and beers to get a script that does the same without exceeding your memory_limit, with ImageMagick however it is just one simple command.

  1. span class=”st0″>"convert inputimage.jpg  -liquid-rescale 250×500%\!  outputimage.jpg");

The nice thing about ImageMagick is that you can often accomplish very sophisticated image effects with just one command, you can also chain commands. Now although IM has a quite good documentation guide available you maybe just want to be able to perform basic image operations with IM without seeking your way trough the documentation all the time. Their are some PHP classes available that will make your life easier.

My personal favor is PhMagick or a home brew script, why? Well I like the idea of wrapper classes more, my code is somewhat more portable because I don’t have to install the IM extension on a server and another PHP extension to communicate with it, furthermore I think that using wrapper classes gives you more insight in IM itself, you will better understand what is going on, on the other hand you probably will spend some hours to figure out why the command is not doing what you expected it to do because you are missing a simple ! or ^ parameter option.

Personal experience on installing IM on the server
Some time ago I wanted to install IM on my new dev and live server, the dev server is running OSX and the live server is running a Fedora distribution. So the first step before I tried to install IM was to read the documentation (yeah I’m one of those guys that also actually reads the readme files and documentation 😉 of products). I followed the “Install from source” tutorial. So I started with the live server, after downloading and unpacking IM I ran the ./configure command immediately I was faced with the error “no acceptable c compiler found in $PATH”, now my Linux skills are far from topnotch, so after some googling I found out that the easiest way to fix this issue was to install GCC (GNU Compiler Collection) that already included a C compiler. After installing GCC and running the ./configure command everything went fine, IM was installed successfully. I wanted to test if it was working correctly so I tried to resize several images but again an error occurred “convert: no decode delegate for this image format”, okay perhaps I should read the documentation more carefully next time because before installing IM you should also install the image libraries that you will use. In my case this was “yum install libjpeg libjpeg-devel libpng libpng-devel libtiff libtiff-devel”, now again I needed to install IM again, not much work, but still something to remember when installing IM. On the dev OSX server installing ImageMagick was quite simple, I used a precompiled dmg and only needed to install the Apple XCode developer tools.

So to summarize:

Advantages of using ImageMagick:

  • it uses less resources, forget about the memory_limit issues with PHP when using GD and large images
  • it’s slighty faster (note: depending on the filetype and image size GD beats IM in speed when it comes to resizing
  • Image Magick has support for more filetypes
  • as I showed before, it is very simple to use
    1. span class=”st0″>’convert "’.$sSourcePath.‘" -resize ‘.$iWidth.‘x"’.$sDestinationPath.‘"’);
  • Some very nice features like liquid scaling or seam carving

Disadvantages of using Image Magick:

  • you need to have full access to your server, it isn’t bundled with PHP
  • compiling IM can give some trouble, you shouldn’t forget to get the image libraries you want to use

Useful links

Written by Sjoerd Maessen

July 19th, 2010 at 7:20 am

Posted in API,Server

Tagged with ,

Leave a Reply