PHP imagick ImagickException no decode delegate for postscript or PDF files


Posted by Andrew McCombe on Fri May 17, 2013

This morning I had an issue with a PHP imagick ImagickException when converting a PDF/postscript file to a thumbnail. I have PHP Imagick, Imagemagick and Ghostscript installed and everything verifies as OK in phpinfo output. The error is:

ImagickException: no decode delegate for this image format `/’ @ error/constitute.c/ReadImage/532

My code:

 $im = new Imagick("$pdf_filename[0]");
 $im->thumbnailImage(150, 220);
 $im->writeimage($thumbnail);

This is a known bug (see http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=11989) and is fixed in the latest releases. Unfortunately the fixes haven’t made it into our server’s packages yet (Ubuntu precise64 / PHP Imagick 3.1.0RC2). The fix is to read the file from a file pointer rather than direct.

$pdf_filename = '01.pdf';
$thumbnail = '01.jpg';
$f = fopen($pdf_filename, 'r');
fseek($f, 0);
$im = new Imagick();
$im->readimagefile($f);
fclose($f);
$im = $im->flattenImages();
$im->setcolorspace(imagick::COLORSPACE_RGB);
$im->setimageformat('pdf');
$im->thumbnailImage(150, 220);
$im->writeimage($thumbnail);

Initially it worked but thumbnails were being generated with black backgounds. Adding the call to “$im->setColorSpace(imagick::COLORSPACE_RGB)” resolved that.

Many thanks to ├ůsmund Herikstad for posting the solution that led me to the issue at http://www.herikstad.net/2010/05/postscript-delegate-failed-for.html