8

I'm using imagick 3.0.1 and also phmagick (http://www.francodacosta.com/phMagick/download). No matter what, both are giving me the same kind of error when I try to convert a PDF to JPEG.

For example: Postscript delegate failed `/tmp/magick-bfxIrUJ5': No such file or directory @ error/pdf.c/ReadPDFImage/669

I'm using Ghostcript-9.05 (running on /usr/local/bin/gs). Also ImageMagick-6.7.6-8 (running on /usr/local/bin/convert).

I already checked folder's permission (755). ImageMagick works wonderful from Terminal.

For example, my PHP using Imagick:

//use imagick
$im = new imagick( '/pdf/553571072.pdf' );

// convert to jpg
$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setImageFormat('jpeg');

//write image on server
$im->writeImage('/jpg/553571072.jpg');

By the way Imagick it's showing up in my phpinfo(). I'm working on linux/apache/amd64.

Any advice will be highly appreciated!

Diego Sarmiento
  • 581
  • 3
  • 7
  • 25
  • running fine from the terminal is irrelevant. You're not running in under Apache's UID then, so it's not a valid test. – Marc B May 04 '12 at 21:16
  • Is your code sample from a web application, or from the command line? And are you really reading from folder '/pdf' and writing to folder '/jpg' i.e. these are both in the root of the filing system? – halfer May 04 '12 at 22:07
  • Halfer, The code is from a web application, I tried also using the complete path '/var/vhost/...../pdf/', same for jpg. The folders are in the base, same as /css and /js. Mark, I don't understand your point, I'm running a web application on a VPS. Thanks guys. – Diego Sarmiento May 04 '12 at 22:17

3 Answers3

17

OK, this finally works, thanks to the help of Nuno Franco da Costa (http://www.francodacosta.com/).

The problem was that my GhostScript path was OK for the Shell, but it was wrong for the System and PHP. To correct that you should link your /usr/local/bin/gs file to /usr/bin/gs.

Do the following from the Shell:

[root@din ~]# convert -list configure | grep -i delegates
DELEGATES     bzlib fontconfig freetype jpeg jng png tiff x11 xml zlib

Check to see if a gs file already exists in /usr/bin. If it does, we'll create a backup of it.
If there is no /usr/bin/gs you can skip this step

[root@din ~]# ls -l /usr/bin/gs
-rwxr-xr-x 1 root root 6024 Sep 30  2010 /usr/bin/gs
[root@din ~]# mv /usr/bin/gs /usr/bin/gs.orig

Now create the link from /usr/local/bin/gs to /usr/bin/gs

[root@din ~]# sudo ln -s /usr/local/bin/gs /usr/bin

That should solve the problem. Thanks a lot guys.

Kevin Jantzer
  • 9,215
  • 2
  • 28
  • 52
Diego Sarmiento
  • 581
  • 3
  • 7
  • 25
  • That solves the problem for me also... but is not possible to specify to php-imagick where the gs executable is? – Arnold Roa Jun 19 '13 at 16:20
  • 3
    I just came through this answer. There is a simple solution. Just add the actual path of 'gs' to the php PATH: putenv(getenv('PATH').':/usr/local/bin') – HelloWorld Aug 23 '13 at 15:04
5

My contribute to the "pure PHP solution" proposed by @HelloWorld (thank you!) in the comments of the accepted answer, a little correction: one must add the variable name before extending the default PATH, so
putenv( getenv('PATH') . ':/usr/local/bin' ); won't work, but
putenv( 'PATH=' . getenv('PATH') . ':/usr/local/bin' ); will

*I have less than 50 points of reputation, couldn't add my comment at the right place, so i had to create a new answer :-)

pikappa73
  • 101
  • 1
  • 1
1

I was getting these problems using ImageMagick ImageMagick 6.7.7-10 with GhostScript 9.10.

It turns out the PDF's were corrupted, and though some newer GS versions could read them, this older version could not, given the following error.

   **** Warning:  An error occurred while reading an XREF table.
   **** The file has been damaged.  This may have been caused
   **** by a problem while converting or transfering the file.
   **** Ghostscript will attempt to recover the data.

Under Linux, you can pass the PDF's through pdftk first:

pdftk 'bad.pdf' output 'fixed.pdf'

After that, everything worked for me on Ubuntu.

Joseph Lust
  • 19,340
  • 7
  • 85
  • 83