How to compile ImageMagick for PHP by hand

Some time ago I was looking at how to re-size uploaded images in a way that looks good using PHP. I was impressed that when I uploaded a 4M picture to flickr it managed to re-size and compress it into a smaller version that looked correct. I knew they weren't just resizing it so I went on a quest to find out what it took to do the same thing with PHP. The following is step one in that process.

My goal was to be able to convert a large image into a smaller one and have it look decent. I tried a few different approaches directly in PHP before I decided to see just how hard it was to do with a command line tool. I found that I could re-size and enhance a photos in a way that made them look pretty good using ImageMagick's re-size and sharpen (for more on doing this on the command line see my ImageMagick command line examples).

At this point I started looking into how to translate what I did on the command line into PHP. The first step was to get access to the ImageMagick libraries into PHP. The first step was to get MagickWand for PHP installed so that I could call the ImageMagick functions with PHP. There are a few other ways of calling ImageMagick out there but they all involved system calls to the convert command and I would rather use library calls. Here are the steps you need to get the MagicKWand extension installed:

  1. Install ImageMagick either from source or a binary. In my case I'm running fedora so I just installed it with yum.
  2. Download the MagickWand extension for PHP.
  3. Go to your PHP source directory and find the ext directory under it. In the ext directory expand the extension.
  4. Change directories into the magickwand directory under ext and run phpize.
  5. Change back to the root PHP source directory and remove the current configuration file: rm -f ./configure
  6. In the root PHP source directory run ./buildconf –force to rebuild the configuration script
  7. You can verify the configuration file was created correctly by looking for magickwand in the new configure script: grep magickwand configure
  8. Now reconfigure your PHP. Use the flag –with-magickwand= to enable MagickWand. For me the directory to ImageMagick was /usr
  9. Now you can recompile PHP and reinstall it. You should find MagickWand listed in a phpinfo() once it is compiled in correctly.

Next time I will go into how to use PHP to do some of the tricks you can do with ImageMagick on the command line.

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *