Installing Imagick under Apache on Windows

After a few weeks of experimenting on and off I’ve just managed to get the Imagick extension installed under Apache on Windows (my development environment). While discovering how to make it work was not at all simple the final process of getting all the pieces to play together nicely was pretty easy. In this post I’ll briefly discuss what ImageMagick and the Imagick extension for PHP are, why you might want to use them, how I got them up and running and finally a very simple piece of example code. I’m hoping this post will help people avoid the frustration and effort I went through to get them to work!

What are ImageMagick and Imagick?

The ImageMagick website has this definition of what it is:

ImageMagick® is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in a variety of formats (over 100) including DPX, EXR, GIF, JPEG, JPEG-2000, PDF, PhotoCD, PNG, Postscript, SVG, and TIFF. Use ImageMagick to translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.

ImageMagick is typically accessed from the command line and is extremely versatile. It can also be accessed from other programming languages to manipulate images and the Windows installation includes optional components to allow C, C++, Perl or Python to access it.

Imagick is a native PHP extension that allows a developer to manipulate images using ImageMagick. It is written and maintained by Mikko Koppanen and is available on Linux/Unix as a PECL extension. Mikko Koppanen also has dll’s available for download from his website but, as I’ll explain in a moment, these can be somewhat problematic for most people to install.

Why use Imagick instead of GD?

Most people use GD for editing images using PHP but I think Imagick offers some real advantages.

  1. Imagick/ImageMagick is supposed to be less resource intensive than GD. I can’t confirm this as I’ve only read it but if true it could be reason enough to switch.
  2. Imagick offers a fully object oriented interface, throwing exceptions on errors. I personally find this much easier to use than using GD’s functions and resources, having to check for problems after each function call. With Imagick any errors raise an exception which is easy to handle.
  3. With Imagick my code seems to be simpler and easier to maintain. Again this is a personal opinion but I find I can achieve the same operations with fewer lines of code using Imagick

The trick of course is to find a hosting company that has Imagick installed but I can think of several here in North America. The other major problem in using Imagick is that the documentation on the PHP website is scarce at best. Hopefully this will improve over time.

Why is installing Imagick on Windows difficult?

Installing Imagick and ImageMagick on Linux/Unix should be pretty easy, I imagine. Installing ImageMagick on Windows is also easy as Windows binaries are available from their website. If you’re running PHP on Windows under IIS installing Imagick should also be a simple matter of downloading the dll files from Mikko Koppanen’s website. Things get tricky if you’re running PHP under Apache on Windows. The problem is that the official Apache binaries available from the Apache foundation are compiled using Visual C 6 (VC6) while the the Imagick dll file from Mikko Koppanen is compiled using Visual C 9 (VC9). These cannot be used together. If you’re running PHP under IIS though this shouldn’t be a problem. The version of PHP that runs under IIS is compiled using VC9 and Mikko Koppanen’s dll file should load without any problems. Someone else has compiled Imagick using VC6 but I could not get this to work properly. I managed to get the extension loaded but it kept crashing with weird errors. After a lot of rooting around I finally found this webpage (in German) that pointed me in the right direction. So, here are the steps I went through to make everything work.

Installing Imagick and ImageMagick

These are the steps I went through to get Imagick and ImageMagick working for me. I can’t promise that it will work for everyone though. Before beginning un-install your current version of Apache and PHP as we’ll be installing a new version. Make sure you keep any config files that you want to use in your new installation though.

  1. Download and install ImageMagick from here. I installed ImageMagick-6.6.2-2-Q16-windows-dll.exe. At the end of the installation follow the instructions and open a command prompt to try out ImageMagick. If everything has worked you should see a copy of the ImageMagick logo. Also try to install ImageMagick somewhere easy to find, like C:\imagemagick which is where I put it.
  2. If you’re running a version of windows that is earlier than Windows 7 install the Visual C++ 2008 redistributable from here.
  3. Download a version of Apache compiled with VC9 from ApacheLounge Apache on Windows page. These are exactly the same as the official Apache binaries but compiled with VC9. According to the website these should have better perfomance, stability and memory management than binaries compiled with VC6.
  4. Install Apache by extracting the files to a location that you want (I use C:\Apache). Start Apache by opening a command prompt, navigating to the bin directory and typing ‘httpd.exe’. At this point if you go to localhost in your browser you should see the ‘It works!’ message. Install Apache as a service (if you wish) by typing ‘httpd.exe -k install’ at the command prompt. Full instructions are included in the files downloaded in step 3.
  5. Download PHP from the official PHP on Windows site and install it, restarting Apache afterwards. Make sure you get binaries compiled with VC9. At this point create a phpinfo() file to verify everything is working.
  6. Download the correct Imagick dll file. Make sure that if you installed a thread safe version of PHP you get the thread safe version and vice versa. Copy the dll file to your PHP ext directory and add a line to your php.ini extensions section to load the extension. Restart Apache again.
  7. At this point if you view the output of phpinfo() you should see that the Imagick extension has loaded.
  8. Install any other custom PHP extensions you need, remembering to use dll’s compiled with VC9.

Hopefully this should work. If anyone has any problems please post here and I’ll try to help. I must emphasise again though that I can’t guarantee this will work for everyone. I can only explain the steps that worked for me.

A Simple Example using Imagick

Here’s a very simple script using Imagick to resize an image. This barely scratches the surface of what the extension can do but it demonstrates how easy it is to perform this very common operation.


<?php
try {
$im = new Imagick();
$im->readImage('test.jpg');
if ($im->getImageHeight() > 500) {
$im->resizeImage(0, 500, Imagick::FILTER_LANCZOS, 1);
}
$im->sepiaToneImage(90.5);
header('Content-type: ' . $im->getFormat());
echo $im->getImageBlob();
}
catch (Exception $e) {
echo $e->getMessage();
}
?>

This code creates a new Imagick object and reads an image in from a file. It then check the image height and resizes it if it is higher than 500px. The Imagick::resizeImage() method needs a little explanation. The first two arguments are width and height. If either of these are left as 0 the image is resized proportionately to the other dimension. The third argument is a ImageMagick resize filter to use (the PHP manual has a full list of all Imagick constants) and the fourth argument details whether to blur or sharpen the image (< 1 is sharpening, > 1 is blurring). The script then converts the image to sepia just for fun before outputting it to the browser. To me this code is far simpler than the equivalent of what would have to be written using GD.

I hope this post has been useful as a brief introduction to Imagick and how I got it to work on Windows, hopefully saving someone the time and frustration I went through. I’ll share more thoughts on using Imagick over the coming weeks.

17 thoughts on “Installing Imagick under Apache on Windows

  1. Thanks for the write-up. I’ve just spent a few hours going through the same mess and got it all going from your instructions.

  2. First, thank you for the write up. I have been trying to get this to work for awhile, and I feel like I am closer than ever. Before I was able to get the whole thing working, but it was with an older version, and it didn’t have a method that I needed. Anyway, did you run into any issues where it wouldn’t process commands and just return

    Error 101 (net::ERR_CONNECTION_RESET): Unknown error.

    Here is the code snippet that I am trying to run

    It just acts like the page doesn’t exist, but if I do

    getCopyright();
    echo $cp
    ?>

    I get

    Copyright (C) 1999-2010 ImageMagick Studio LLC

    and yes the file exists. Anyway, any clues I have tried different versions of Image Magick (all Q16 windows.dll)

    Any ideas?

    1. Hi Dan,

      I’m not totally sure what could be causing your issues as WordPress seems to have mangled most of your code examples. I did run into a few issues when I was testing out Imagick that led to errors like the one you mention (as far as I can remember). There were two problems from what I remember. The first was that some of the Imagick methods I was using (which were from a tutorial) were deprecated. This normally wouldn’t be a problem but as this is my development box I have error reporting turned up to E_ALL and display errors turned on. As a result PHP was dumping error message to the output which should have been an image file. This was making the image files invalid, causing the browser to issue cryptic errors. Does that sound like it might be your problem? I’d try doing some very simple operations on an image as a test to make sure it seems to be working. If that goes fine I’d have a look at the methods you’re trying to call to make sure none of them are deprecated.

      One final thing. Sorry to ask this but you do have all of the correct pieces in the puzzle (Apache, PHP and Imagick compiled with VC9)? Just making sure!

      Good luck.

  3. Hello Jeremy!

    Thanks for your tutorial. I just wanted to advise something to anyone during the install of ImageMagic > 6.4.X

    It doesn’t work. For anyone wanting to follow your guide, please take notice of Chris comment at http://valokuva.org/?p=161#comment-20707.

    Install a former version of Imagemagick will only work.

    In my case Apache completly crashed and restarted. With ImageMagick 6.3.9 everything went fine.

    Thanks!

    Luis

    1. Hi Luis,

      Thanks for the update. I wasn’t aware of that issue as I’m running ImageMagick 6.6.3-4. It’s good to know about that problem before I try to upgrade though. Looks like the whole depressing cycle of which build to use is starting all over again!

      Thanks again,

      Jeremy.

  4. Thank you so much for the write-up… found a decent amount of information on getting ImageMagick to run under windows, but wasn’t able to actually get it going until your article.

    I got mine going with XAMPP and Windows 7. There are older VC9 versions of XAMPP available on sourceforge, I used 1.7.7 which is available here: http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.7.7/
    Specifically I downloaded xampp-win32-1.7.7-VC9-installer.exe, which is thread-safe.

    For ImageMagick I used ImageMagick-6.6.3-4-Q16-windows-dll.exe from http://image_magick.veidrodis.com/image_magick/binaries/

    For the extension I used ‘2011-04-25 18:49 – php_imagick_ts.dll’ from
    http://valokuva.org/builds/

    I also installed ghostscript and it’s working fine as well.

    Again, thanks for your write-up!

    1. Hi John,

      I followed the steps and i installed files. After restart the Apache I got the warning msg like PHP Startup: imagick: Unable to initialize module. Any solution for this?

  5. John,

    you just made my day. For hours and hours and hours I have been fighting like a madman to get Imagick going on my Windows 7 x64 machine, with Xampp.

    I tried at least 100 versions, from 100 forum posts. Your’s finally worked.

    You made my day.

    Thanks a lot!

  6. Hi
    I want to install the ImageMagick . Currently am using Windows XP service pack-2 and am using PHP 5.3.5 version .. so can you please tell me which dll is support for this…

    1. Hi Karthikphp, unfortunately I can’t advise you any more than what’s in the article. I’m using Linux as my OS these days and obviously things are somewhat different on that. Good luck in getting it sorted out.

  7. Hello thanks for the post i installed the extensions and its showing and checked in the php extensions list , but when i try your sample code i get “Class ‘Imagick’ not found in C:\wamp\www\imagick.php on line 3″ i am using wamp on windows 7 any help please

Leave a Reply