0
votes

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="&quot;@PSDelegate@&quot; \
   -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
   -dEPSCrop \
   -sDEVICE=epswrite &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>

<delegate decode="pdf" encode="ps" mode="bi" \
    command="&quot;@PSDelegate@&quot; \
   -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
   -dEPSCrop -dAlignToPixels=0 -dGridFitTT=2 \
   -sDEVICE=pswrite &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>

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
2
Can you add -verbose to your successful PDF conversion command? Run convert -verbose rose.pdf rose.jpg. You'll see the Ghostscript command which is called by ImageMagick. Maybe this gives you a hint about what you need to change in your Imagick setup...Kurt Pfeifle
Strange... I find the [ghostscript library] Files/gs/gs9.15/bin/gswin64c.exe" part a bit strange. Are you sure you copy'n'pasted it correctly?!? -- I'm guessing this should be "C:/Program Files/gs/gs9.15/bin/gswin64c.exe"Kurt Pfeifle
yes , its copy'n'pasted including [ghostscript library] I had the same doubt , i tried with Files/gs/gs9.15/bin/gswin64c.ex but i had no success , i will try now with C:/Program filesGuillermo Nahuel Varelli
"I assume that..." don't assume. What is the actual error in your log file?Danack
I dont have errors when i run convert -verbose 2.pdf 1.jpg , but when i use the php code to convert the PDF the page goes down.Guillermo Nahuel Varelli

2 Answers

1
votes

It works for me :). I had already tried an installation there a few months without success.

I simply followed that topic : Where to find php_imagick.dll for php 5.5.12 for Windows wampserver 2.5?

And after, I've copy/paste the C:/Path/to/ImageMagick/delegates.xml in C:/wamp/bin/php/php.5.5.12/ext/imagick and edit the delegate tag for pdf :

<delegate decode="eps" encode="pdf" mode="bi" command="&quot;C:\Program Files (x86)\gs\gs9.15\bin\gswin32c.exe&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -sDEVICE=pdfwrite &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>
0
votes

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="&quot;/opt/local/bin/gsx&quot; \
 -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=epswrite&quot; \
  &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;"/>

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

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

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

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