How to install, test, convert, resize PDF using ImageMagick, Ghostscript, Windows Vista/7 x64
Asked Answered
A

3

14

I'm having trouble with getting ImageMagick and Ghostscript to work together - everything seems difficult, from installation, to testing, to actually using the software. Does anyone have any tips?

Abohm answered 14/7, 2010 at 4:19 Comment(3)
Thanks, that is a nice review. Are there any permission things I need to be aware of? I'm running WAMP and I get nothing when my script runs. From the Windows command prompt it works great. It seems like it doesn't run through Apache.Buonaparte
Uhm, do you want to make that a separate Question / Answer, rather than putting everything into the question? Thanks.Daigle
OK. This was actually my first contribution to SO, didn't know how things worked, but that's a good idea. :)Abohm
A
40

I've just spent the last three hours getting ImageMagick to play nicely with PHP for a PDF to JPG convert. Instead of documenting it myself locally, I figured I'd write it up here, with all the buzzwords and things I screwed up, so the next noob can save some time.

Tested on two OSs,Windows 7 x64 and Vista x64, with PHP 5.2.8 and Apache 2.2.11.

You'll need Ghostscript and ImageMagick. I got gs871w64.exe and ImageMagick-6.6.3-0-Q16-windows-x64-dll.exe.

Do a basic install of ImageMagick (I'll call it IM now). Double check the system path variable to ensure you can IM from anywhere. It should point to the IM directory. Test the installation with this line in cmd: convert test.gif test.jpg (assuming you have test.gif). Works OK.

Test a PDF convert (convert test.pdf test.jpg). You should get a (long) message about "gswin32c.exe" and "Postscript delegate failed" and "missing an image filename". That means IM can't find a PDF 'translator'. You need Ghostscript.

Install Ghostscript (I'll call it GS now). I had to do it twice for some reason before it took. Add the 'bin' address in the GS folder to the system variable. Test the installation with gswin32 test.pdf and you should see your file pop up in the GS viewer. Important note: gs doesn't work in Windows ('gs' is not recognized as an internal or external command, operable program, or batch file.). You need to use gswin32.

Back to the IM test: convert test.pdf test.jpg should work now.

Now over to PHP - write up this script:

$out = shell_exec("convert blah.pdf blah2.jpg 2>&1");
echo $out;

The 2>&1 will move all errors into the standard output so you can better diagnose stuff :). Now, your PHP script may run in the cmd prompt, but nothing happens when you run it in a browser. Smack your forehead and restart Apache.

pant Exhaustive and probably elementary, I know. But a hopefully helpful summary of approximately 20 pages of semi-helpful clues.

Abohm answered 30/11, 2011 at 4:30 Comment(1)
convert command on windows is for change format type from normal to ntfs. magick convert can be used instead but it is giving error "magick is not known command" i have it working and in path otherwise. thanksSouthard
S
11

Steve already provided a guide on running the command line version of ImageMagick from PHP. After having a similar experience installing the extension version of ImageMagick, I'd like to elaborate a bit on how the different components work together.

Imagick (the PHP extension)

First you'll need the PHP extension. It's basically an adapter between PHP and the ImageMagick functions.

  • Choose a version from http://windows.php.net/downloads/pecl/releases/imagick/
  • Download the extension matching your PHP version in architecture (probably 32bit), thread-safe-ness and compiler (VC9 or VC11), see phpinfo
  • Copy php_imagick.dll from the extension ZIP to the PHP extension directory

However, this is not enough. The php_imagick.dll does not contain any ImageMagick functionality and the CORE_RL_... DLLs that come bundled with the extension are not complete. You're gonna need some more DLLs from an ImageMagick release as well.

Theoretically the version doesn't have to match exactly, but apparently somewhere between ImageMagick 6.8.1 and 6.8.8 the function MagickGetImageMatte has been removed from the DLLs, so the safest way is to find the exact same ImageMagick release that the Imagick extension was build for:

  • In Windows Explorer, see Properties → Details of the bundled CORE_RL_wand_.dll to find the version of ImageMagick that this extension is made for. (You should also be able to find this information in phpinfo.)
  • Download that exact ImageMagick version, for example from http://windows.php.net/downloads/pecl/deps/ (thanks to this guy for the link)
  • Copy all the DLLs from the ImageMagick ZIP to the same directory where php5(n)ts.dll (the PHP engine for Apache) is

It is not necessary to install ImageMagick. In fact, if you happen to have an incompatible ImageMagick in your PATH, the PHP extension might fail.

Ghostscript

This applies to both the command line version of ImageMagick (convert) and the PHP extension described above.

For reading files of the Postscript family, Ghostscript is needed.

ImageMagick will find Ghostscript following these steps:

  • If there is a GS_DLL entry in the registry, pointing to the path of gswin32.dll, it will use that one and ignore the delegates altogether.
  • Otherwise it will consult it's "delegates". There seems to be a hardcoded fallback, but you can override it by putting a delegates.xml next to php5(n)ts.dll or convert.exe respectively or in ~\.magick.
  • If it finds the string @PS_Delegate@ in the delegate, it will replace it with gswin32c.exe and then continue looking for that EXE in the PATH. You can replace this string with the full path to gswin32c.exe or even gswin64c.exe that ImageMagick should call.
Streetcar answered 23/8, 2015 at 5:56 Comment(1)
I placed C:\ghostscript\bin` in my PATH variable in Windows, but still I have got Uncaught exception 'ImagickException' with message 'NoDecodeDelegateForThisImageFormat` when I call readImageFile(...)Woolery
C
1

I was facing the same issue I installed Ghostscript 9.54.0 for Windows (64 bit) it solved my issue:

Followed the following steps:

  1. Go to https://www.ghostscript.com/download.html clicked Ghostscript under "Postscript and PDF interpreter/renderer:". It opened a new page: https://www.ghostscript.com/download/gsdnld.html

  2. From the page installed Ghostscript 9.54.0 for Windows (64 bit).

  3. After downloading install it on the windows machine.

After that restated kernel and re-executed jupyter notebook.

Cassilda answered 15/7, 2021 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.