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:
- Install ImageMagick either from source or a binary. In my case I'm running fedora so I just installed it with yum.
- Download the MagickWand extension for PHP.
- Go to your PHP source directory and find the ext directory under it. In the ext directory expand the extension.
- Change directories into the magickwand directory under ext and run phpize.
- Change back to the root PHP source directory and remove the current configuration file: rm -f ./configure
- In the root PHP source directory run ./buildconf –force to rebuild the configuration script
- You can verify the configuration file was created correctly by looking for magickwand in the new configure script: grep magickwand configure
- Now reconfigure your PHP. Use the flag –with-magickwand=
to enable MagickWand. For me the directory to ImageMagick was /usr
- 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.