Error Imagick / Ghostscript with PDF entry

I have installed ImageMagick on Windows with the DLL for imagick PHP.

I need to convert PDFs into JPEGs. So I also installed Ghostcript.

If I run this command:

convert rose.pdf rose.jpg

it works well. But if I try to reproduce this same functionality in my web environment with PHP:

$im = new imagick('test_pdf.pdf[0]' ); 

$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100);
$im->setImageFormat('pdf');
$im->writeImage('thumb.jpg');

then my page goes down.

I assume that Imagick PHP fails with PDF to JPEG, because it doesn't know how to use the Ghostscript library gslib .

With this code, however, I can convert PNG to JPEG.

I tried to edit in delegates.xml but I'm not exactly sure how this should be done.

Here are the relevant lines from my delegates.xml :

<delegate decode="pdf" encode="eps" mode="bi" \
    command=""@<a href="/cdn-cgi/l/email-protection" data-cfemail="540407103138313335203114">[email protected]</a>" \
   -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
   -dEPSCrop \
   -sDEVICE=epswrite "-sOutputFile=%o" -- "%i""/>

<delegate decode="pdf" encode="ps" mode="bi" \
    command=""@<a href="/cdn-cgi/l/email-protection" data-cfemail="da8a899ebfb6bfbdbbaebf9a">[email protected]</a>" \
   -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
   -dEPSCrop -dAlignToPixels=0 -dGridFitTT=2 \
   -sDEVICE=pswrite "-sOutputFile=%o" -- "%i""/>

Update

I tried to run the ImageMagick with -verbose in order to get additional hints:

convert -verbose 2.pdf 1.jpg

Result:

[ghostscript library] Files/gs/gs9.15/bin/gswin64c.exe" -q -dQUIET -dSAFER -dBATCH
-dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dEPSCrop -dAlignToPixels=0 -dGridFitTT=2
 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72"
 "-sOutputFile=C:/Users/Usuario/AppData/Local/Temp/magick-PjagmlB-%08d"
 "-fC:/Users/Usuario/AppData/Local/Temp/magick-1l5fdY8X"
 "-fC:/Users/Usuario/AppData/Local/Temp/magick-Fr-GsA3"
  C:/Users/Usuario/AppData/Local/Temp/magick-_PjagmlB-000000
 1 PNG 609x791 609x791+0+0 8-bit DirectClass 47.8KB 0.016u 0:00.031
 2.pdf PDF 609x791 609x791+0+0 16-bit DirectClass 47.8KB 0.000u 0:00.031
 2.pdf=>1.jpg PDF 609x791 609x791+0+0 16-bit DirectClass 131KB 0.031u 0:00.046 –

Result from Apache error Log

[Mon Dec 22 09:11:59.022854 2014] [mpm_winnt:notice] [pid 6600:tid 520] AH00428: Parent: child `process 6552 exited with status 255 -- Restarting.
[Mon Dec 22 09:11:59.170587 2014] [mpm_winnt:notice] [pid 6600:tid 520] AH00455: Apache/2.4.9 (Win64) PHP/5.5.12 configured -- resuming normal operations -- PHP/5.5.12 configured -- resuming normal operations
[Mon Dec 22 09:11:59.170587 2014] [mpm_winnt:notice] [pid 6600:tid 520] AH00456: Apache Lounge VC11 Server built: Mar 16 2014 12:42:59
[Mon Dec 22 09:11:59.170587 2014] [core:notice] [pid 6600:tid 520] AH00094: Command line: 'c:\\wamp\\bin\\apache\\apache2.4.9\\bin\\httpd.exe -d C:/wamp/bin/apache/apache2.4.9'
[Mon Dec 22 09:11:59.170587 2014] [mpm_winnt:notice] [pid 6600:tid 520] AH00418: Parent: Created child process 4484
[Mon Dec 22 09:11:59.807339 2014] [mpm_winnt:notice] [pid 4484:tid 436] AH00354: Child: Starting 64 worker threads.

I made a var_dump when the PDF its loaded

$im = new imagick( __DIR__ . DIRECTORY_SEPARATOR .'test_pdf.pdf' );
 var_dump($im);die;

But the page goes down before enter in var_dump.

TEST SCENARIOS

  • PDF to JPG using PHP on the CommandLine > works
  • PDF to JPG using PHP-FPM from browser > doesn't work (error above)
  • JPG to PDF using PHP-FPM from browser > works

Here are the relevant lines from my delegates.xml . I do however not use Imagick nor PHP, so I'm not sure it's the only change you need to make in order to make it work.

First, a form that lets you read it without horizontal scrolling. You have to remove the line breaks as well as the line continuation characters \ which I inserted:

<delegate decode="pdf" encode="eps" mode="bi" \
  command=""/opt/local/bin/gsx" \
 -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" \
  "-sOutputFile=%o" "-f%i""/>

<delegate decode="pdf" encode="ps" mode="bi" \
  command=""/opt/local/bin/gsx" \
 -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ps2write" \
  "-sOutputFile=%o" "-f%i""/>

Now the original lines. You need to scroll horizontally to see it fully and in its original shape:

<delegate decode="pdf" encode="eps" mode="bi" command=""/opt/local/bin/gsx" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i""/>
<delegate decode="pdf" encode="ps" mode="bi" command=""/opt/local/bin/gsx" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ps2write" "-sOutputFile=%o" "-f%i""/>

These lines are from my Mac OS X version of ImageMagick, 6.9.0-0 Q16 x86_64 2014-12-06 .

As you can see, it doesn't use a @PSDelegate -syntax. Instead it contains the full path to the Ghostscript executable, in my case /opt/local/bin/gsx .

For your case, it should possibly be the full Windows path to your Ghostscript executable, which usually is named gswin32c.exe or gswin64c.exe .

Your problem with the strange @PSDelegate possibly comes from the fact that you installed ImageMagick/Imagick before you installed Ghostscript, and installed Ghostscript only afterwards....

Update

Maybe you should try for your Windows/Ghostscript installation the following lines in your delegates.xml file:

<delegate decode="pdf" encode="eps" mode="bi" command=""C:/Program Files/gs/gs9.15/bin/gswin64c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i""/>
<delegate decode="pdf" encode="ps" mode="bi" command=""C:/Program Files/gs/gs9.15/bin/gswin64c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ps2write" "-sOutputFile=%o" "-f%i""/>

Note , you do not need to use backslashes (' \ ') -- Ghostscript and ImageMagick are just fine with forward slashes (' / ') on Windows.