North Central Washington

Web Development


ImageMagick on XAMPP with Ghostscript


Getting ImageMagick and Ghostscript to work with PHP and XAMPP

ImageMagick is a great program for transforming image files and formats. IM even has the ability to convert PDF files to images if Ghostscript is also installed on your system. Unfortunately, installing and running IM the first time is highly system dependent. What works on one system likely won't work on another. This web page describes an example of IM, Ghostscript, PHP and XAMPP all working together on a local Windows XP system.

These instructions also might be helpful if you are trying to control IM and Ghostscript on a remote host. In that case, you would need those two programs installed on the remote server and you may also need to know the path to the IM executable. IM wants to be called from its full path name, at least on Windows. On the plus side, IM doesn't need a full path to find Ghostscript, making conversion of pdf files to images no more difficult than converting between images.

1. This example assumes you are using XAMPP and the localhost server is located at c:/xampp/htdocs/

2. Download and install Ghostscript. The windows installer for Ghostscript is available at http://sourceforge.net/projects/ghostscript/ or http://pages.cs.wisc.edu/~ghost/. I changed the default install directory during installation to a more convenient short path (c:/prog/gs/), but it probably doesn't matter since on this system IM can find Ghostscript without any fuss.

3. Download and install IM. I used the so-called Windows Binary Release with the name ImageMagick-6.5.8-10-Q16-windows-dll.exe. It was available for download at http://www.imagemagick.org/script/binary-releases.php. When installing, it does make a difference where IM gets installed, because the location of the IM executable needs to be sent to CMD.EXE (DOS), e.g., via the PHP exec_shell command. Without a path to IM, PHP won't show any error message, nor will anything happen, either (strangely, you can call the IM convert command from a DOS prompt without a full path). Also, when DOS is called, it might get into a snit if there are spaces or too many characters in the default folders in the pathname. Besides that, the default location on Windows is too long for convenient use. So it is probably better to install to a short 8.3 DOS-compatible folder, e.g., c:/prog/magick/.

The PHP code for calling IM needs some way of confirming whether it is working or not to make up for the lack of error messages if IM fails to execute. The following code block provides three status messages. The first message lists files in the current folder, the second message calls IM and prints the string sent to DOS, the third message lists the files again, which should have changed if the call to IM was successful. In the following example, IM converts test.pdf to test7.jpg with a height of 100 pixels. This is shown by the second file listing that shows test7.jpg has been created.

PHP code:


  $dirlist='dir /o/a:-d' ; // DOS directory list command
  echo '<b>Initial Directory Listing: </b>' .$dirlist ;
  $result = shell_exec($dirlist) ;
  echo "<pre>" .$result ."</pre>" ;

  $magick_dir = 'C:\prog\magick' ; // install IM in short DOS 8.3 compatible path
  $send_cmd=$magick_dir .'\convert test.pdf -resize x100 test7.jpg' ;
  echo '<b>Execute command: </b>' .$send_cmd ;
  $result = shell_exec($send_cmd) ;
  echo "<pre>" .$result ."</pre>" ;

  echo '<b>New Directory Listing: </b>' .$dirlist ;
  $result = shell_exec($dirlist) ;
  echo "<pre>" .$result ."</pre>" ;

The output should look like this:

Initial Directory Listing: dir /o/a:-d
 Volume in drive C is SOS
 Volume Serial Number is 46EA-AA5F

 Directory of C:\xampp\htdocs\test

01/10/2010  12:54 PM               607 index.php
01/04/2010  02:43 AM           137,301 test.pdf
               2 File(s)        137,908 bytes
               0 Dir(s)   5,658,914,816 bytes free

Execute command: C:\prog\magick\convert test.pdf -resize x100 test7.jpg

New Directory Listing: dir /o/a:-d
 Volume in drive C is SOS
 Volume Serial Number is 46EA-AA5F

 Directory of C:\xampp\htdocs\test

01/10/2010  12:54 PM               607 index.php
01/04/2010  02:43 AM           137,301 test.pdf
01/10/2010  12:55 PM             2,783 test7.jpg
               3 File(s)        140,691 bytes
               0 Dir(s)   5,658,910,720 bytes free

I hope this helps someone.

- Not responsible for any damages. Always backup files before tinkering!

Happy button-clicking - George Wooten