Page 1 of 1

Uncaught 'ImagickException' reading PDF with Ghostscript in PHP

Posted: 2016-07-16T10:16:08-07:00
by Crispin
Hi folks,

I've for php 5.6.23 running in a freebsd jail with imagemagick installed.

I have a file (777) in the root called in.php.
If I run the following in a command line it converts it fine

Code: Select all

convert in.pdf out.png
If I run the following code, I get an exception:

Code: Select all

$path = '/mnt/web/LandCruiserClub/in.pdf';

print "Readable:" . is_readable($path) . "|<br>";
print "path: " . $path . "<br />";
print "file exists." . file_exists($path) . "<br />";

$pdf = new Imagick($path.'[0]');
$pdf->setimageformat('jpg');
$pdf->writeimage('/mnt/web/LandCruiserClub/out.jpg');
If I try with fully qualified and relative paths.

Code: Select all

Fatal error: Uncaught exception 'ImagickException' with message 'Failed to read the file' in /mnt/web/LandCruiserClub/pi.php:18 Stack trace: #0 /mnt/web/LandCruiserClub/pi.php(18): Imagick->__construct('in.pdf[0]') #1 {main} thrown in /mnt/web/LandCruiserClub/pi.php on line 18
Any help with why this might be happening?


Thanks

Image Magick:

Code: Select all

imagick

imagick module	enabled
imagick module version	3.4.1
imagick classes	Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel
Imagick compiled with ImageMagick version	ImageMagick 6.9.4-3 Q16 amd64 2016-07-14 http://www.imagemagick.org
Imagick using ImageMagick library version	ImageMagick 6.9.4-3 Q16 amd64 2016-07-14 http://www.imagemagick.org
ImageMagick copyright	Copyright (C) 1999-2016 ImageMagick Studio LLC
ImageMagick release date	2016-07-14
ImageMagick number of supported formats:	229
ImageMagick supported formats	3FR, AAI, AI, ART, ARW, AVI, AVS, BGR, BGRA, BGRO, BIE, BMP, BMP2, BMP3, BRF, CAL, CALS, CANVAS, CAPTION, CIN, CIP, CLIP, CMYK, CMYKA, CR2, CRW, CUR, CUT, DATA, DCM, DCR, DCX, DDS, DFONT, DNG, DOT, DPX, DXT1, DXT5, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EPT2, EPT3, ERF, FAX, FITS, FPX, FRACTAL, FTS, G3, GIF, GIF87, GRADIENT, GRAY, GROUP4, GV, H, HALD, HDR, HISTOGRAM, HRZ, HTM, HTML, ICB, ICO, ICON, IIQ, INFO, INLINE, IPL, ISOBRL, ISOBRL6, J2C, J2K, JBG, JBIG, JNG, JNX, JP2, JPC, JPE, JPEG, JPG, JPM, JPS, JPT, JSON, K25, KDC, LABEL, M2V, M4V, MAC, MAGICK, MAP, MASK, MAT, MATTE, MEF, MIFF, MKV, MNG, MONO, MOV, MP4, MPC, MPEG, MPG, MRW, MSL, MSVG, MTV, MVG, NEF, NRW, NULL, ORF, OTB, OTF, PAL, PALM, PAM, PANGO, PATTERN, PBM, PCD, PCDS, PCL, PCT, PCX, PDB, PDF, PDFA, PEF, PES, PFA, PFB, PFM, PGM, PICON, PICT, PIX, PJPEG, PLASMA, PNG, PNG00, PNG24, PNG32, PNG48, PNG64, PNG8, PNM, PPM, PREVIEW, PS, PS2, PS3, PSB, PSD, PTIF, PWP, RADIAL-GRADIENT, RAF, RAS, RAW, RGB, RGBA, RGBO, RGF, RLA, RLE, RMF, RW2, SCR, SCT, SFW, SGI, SHTML, SIX, SIXEL, SPARSE-COLOR, SR2, SRF, STEGANO, SUN, SVG, SVGZ, TEXT, TGA, THUMBNAIL, TIFF, TIFF64, TILE, TIM, TTC, TTF, TXT, UBRL, UBRL6, UIL, UYVY, VDA, VICAR, VID, VIFF, VIPS, VST, WBMP, WEBP, WMF, WMV, WMZ, WPG, X3F, XBM, XC, XCF, XPM, XPS, XV, YCbCr, YCbCrA, YUV

Re: Uncaught exception 'ImagickException' with message 'Failed to read the file'

Posted: 2016-07-16T10:30:15-07:00
by snibgo
Does it work from PHP with any conversions from PDF? If not, the problem may be accessibility of Ghostscript from PHP. Try modifying your delegates.xml to include the full path to Ghostscript.

Re: Uncaught exception 'ImagickException' with message 'Failed to read the file'

Posted: 2016-07-16T11:01:11-07:00
by Crispin
No, no conversions work with any pdf.

urm, what is delegates.xml? (off to try find out in the hopes it makes sense.)

Re: Uncaught exception 'ImagickException' with message 'Failed to read the file'

Posted: 2016-07-16T11:15:32-07:00
by Crispin
I removed ghostscript package and reinstalled it. It wants to install the following:

Code: Select all


New packages to be INSTALLED:
        pecl-imagick: 3.4.1
        ImageMagick-nox11: 6.9.4.3,1
        ghostscript9-agpl-base: 9.16_5
Does this look right?

if I give it a bad path on purpose I get a different error -

Code: Select all

Uncaught exception 'ImagickException' with message 'unable to open image `/mnt/web/LandCrussiserClub/in.pdf': 
I can't find anything about delegates.xml - where would I find that?

Re: Uncaught exception 'ImagickException' with message 'Failed to read the file'

Posted: 2016-07-16T11:21:18-07:00
by snibgo
I don't use PHP so my diagnosis is only a guess based on what other have said in these forums. Try a search to find posts on very similar problems.

The directory that contains your "convert" program has a text file called delegates.xml, which tells convert how to run Ghostscript, to read PDF files. But "running a program" under PHP works differently to doing it from the command line. The problem is the system path, or something.

Re: Uncaught exception 'ImagickException' with message 'Failed to read the file'

Posted: 2016-07-16T11:26:36-07:00
by Crispin
found delegates in /usr/local/etc/ImageMagick-6/delegates.xml

However, it has nothing about ghostscript. All I can see for dpf is this:

Code: Select all

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

Re: Uncaught exception 'ImagickException' with message 'Failed to read the file'

Posted: 2016-07-16T11:33:03-07:00
by snibgo
You found it: ""gs"".

"gs" is a common name for Ghostscript. Try changing that to ...

Code: Select all

"/bin/my/path/to/gs"
... with the correct path, as found by "where" or "whereis" or whatever.

Re: Uncaught exception 'ImagickException' with message 'Failed to read the file'

Posted: 2016-07-17T13:53:18-07:00
by Crispin
thanks for the tip on gs.

I've put the full path in and it still does not work :( Still get the same error as always :(

Re: Uncaught exception 'ImagickException' with message 'Failed to read the file'

Posted: 2016-07-17T14:25:33-07:00
by Crispin
And we've solved it.

ln -s /usr/local/bin/gs /usr/bin/gs

Even putting the full path in the delegates.xml seemed to have no effect.

Sym-linking did the trick.

thanks for the help trying to fix it though - much appreciated.

Re: Uncaught 'ImagickException' reading PDF with Ghostscript in PHP

Posted: 2016-07-17T16:34:01-07:00
by snibgo
Glad you fixed it. And thanks for posting your solution, it may help other people.

I've taken the liberty of changing the thread title, to help search engines find it.

Re: Uncaught 'ImagickException' reading PDF with Ghostscript in PHP

Posted: 2016-07-18T00:14:19-07:00
by Crispin
Thanks for changing the title. Hopefully it does help someone else.