0

I was using Imagick to generate thumbs in JPEG from pages of a PDF document. My local server for test never be able to do it, I always got a Ghostscript error. I was developing directly in my website host but now it stop working, it seems since 6.9.0 version of ImageMagick. As it is a shared hosting, I cannot do a downgrade and the administrators do not wish to do it (will be a donwngrade for everyone in the same server). It's a simple cheap hosting plan, I don't think they want to help me...

Also, as I look for answers, I see a lot of people complainig about this version, so it looks like a real bug.

So I started to look for an alternative inside PHP libraries or some API I can install to perform the job. Any suggestions?

This is the line that generates the error:

$Img->readImage($urlArq."[".$pag."]");

The error:

Fatal error: Uncaught exception 'ImagickException' with message 'PDFDelegateFailed `[ghostscript library] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r150x150" -dFirstPage=1 -dLastPage=1 "-sOutputFile=/tmp/magick-316787_tM5qhaBA3E%d" "-f/tmp/magick-316787MBoQDwPMLvz" "-f/tmp/magick-31678SNvHB_Zdq8Xt"': -dname= must be followed by a valid token @ error/pdf.c/InvokePDFDelegate/263' in /home/sintrano/public_html/conteudo_apitaco_jpeg.php:23 Stack trace: #0 /home/sintrano/public_html/conteudo_apitaco_jpeg.php(23): Imagick->readimage('apitaco/edicao-...') #1 {main} thrown in /home/sintrano/public_html/conteudo_apitaco_jpeg.php on line 23

The version info:

versionNumber: 1680
versionString: ImageMagick 6.9.0-0 Q16 x86_64 2016-05-05 http://www.imagemagick.org

EDIT: I found this bug in my code: $pag cannot be zero, the first page is 1. But this was not generating the error before, don't know why, and I'm not sure if this was the problem.

Gustavo
  • 1,673
  • 4
  • 24
  • 39
  • The error with Ghostscript seems to be a problem with the version of ImageMagick you have installed, see this question: http://stackoverflow.com/questions/38272126/error-trying-to-create-a-jpeg-thumb-image-of-a-pdf-using-imagick-on-godaddy-serv – KenS Jul 22 '16 at 07:34
  • In my tests, Ghostscript is not accepting zero as first page of a PDF document, got to be "1" (I did answer this question you mention). As my code was old, I am assuming old versions of Ghostscript do or Imagick was doing the simple correction (0 = 1) before calling the library. – Gustavo Jul 26 '16 at 14:12

1 Answers1

1

if you have dedicated server or vps you can use convert sudo apt-get install convert and then convert infile.pdf outfile.png there are a lot of things you can do before export to jpg/png convert man page

you can use exec exec function from php to run convert and then get your files by file_get_contents file_get_contents and readdir readdir

neuronet
  • 1,139
  • 8
  • 19
  • I am not the administrator, I'm using a shared hosting service (small website). I don't think will be allowed to me go further in command line. However, maybe I can get something by exec function, maybe at list to test ghostscript directly... – Gustavo Jul 22 '16 at 13:28
  • I put +1 to this answer because I could avoid the problem by creating my own temporary jpeg file from pdf by calling ghostscript by command line using exec. So I can do at list a part of the job outside. But I'll keep this question open because I think is interesting to explore other ways to do Imagick job. Also see an important Edit above. – Gustavo Jul 26 '16 at 14:01
  • Well, since nobodyelse try another answer, I will mark this one as correct because it is correct. But exec is a bit tricky for noobs (like me). In my case, I did use ghostscript (gs command line) to create a temporary jpeg file that can be loaded by php imagecreatefromjpeg function and worked with GD library, avoiding ImageMagick Imagick completely. – Gustavo Aug 05 '16 at 18:08