Wednesday, May 5, 2010

Postscript delegate failed for ImageMagick PHP library

For ImageMagick in PHP through the library Imagick the following error may occur for versions ImageMagick-6.3.5 to ImageMagick-6.4.2:
Postscript delegate failed `': No such file or directory

This is a bug and the error appears when one is trying to use the readimageblob function to read a PDF file.

The fix has been posted and the bug is no longer present. See http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=11989. So to fix it get the latest release from http://www.imagemagick.org/.

Some installations (like server environments), haven't got the latest version and cannot install it. For these, it is possible to work around this bug, by using a temp file.

So instead of:
$im = new Imagick();
$im->readimageblob($data);


Use:
$im = new Imagick();
$temp = tmpfile();
fwrite($temp, $data);
fseek($temp, 0);
$im->readimagefile($temp);
fclose($temp);


(This would typically be followed by for example:
$im->setImageFormat("png");
$data = $im->getimageblob();
)

Sources: http://markmail.org/message/6mg3lxi26gd5fluv#query:readimageblob%20Postscript%20delegate%20failed+page:1+mid:6mg3lxi26gd5fluv+state:results

If this doesn't fix it for you, also check out: http://stackoverflow.com/questions/588918/imagemagick-imagick-convert-pdf-to-jpg-using-native-php-api

4 comments:

  1. Hi, in what do you put in $data?
    Something like
    $data = file_get_contents("test.pdf"); ?
    Because even with this I have the same problem of missing file!

    ReplyDelete
  2. Hi

    In this case I was working with a file that was uploaded to the server and then saved in my database in a BLOB field (see http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx for an example). So the content of a file would be the result of the database statement.

    If you want to read data from a file (not a temp file like here) you would create the file handle $temp using the fopen function (http://php.net/manual/en/function.fopen.php , also see http://www.php.net/manual/en/function.imagick-readimagefile.php).
    When making the file handle directly, make sure the file is actually in the right folder. Use the file_exists function (http://php.net/manual/en/function.file-exists.php) and make sure you are looking the right place (try "echo basename(dirname(__FILE__));" to see where you are, and have a look at http://www.computing.net/answers/webdevel/php-current-folder/2956.html)

    ReplyDelete
  3. First of all thanks for answering.
    I'm trying with this code

    /* FILE PATH */
    $pdf_filepath = "test.pdf";
    /* DESTINATION THUMBAIL */
    $thumbnail_filepath = "test.jpg";

    $im = new Imagick();

    if( file_exists($pdf_filepath) )
    $data = fopen($pdf_filepath,"rb");
    $temp = tmpfile();
    fwrite($temp, $data);
    fseek($temp, 0);
    $im->readImageFile($temp);
    fclose($temp);

    $im->thumbnailImage(150, 240);
    $im->setImageFormat('jpg');
    $result = $im->writeImage($thumbnail_filepath);

    And there comes out a new error.

    Fatal error: Uncaught exception 'ImagickException' with message 'no decode delegate for this image format `/tmp/magick-XXyyfNSm'' in /addons/jpgtopdf.php:73 Stack trace: #0 /addons/jpgtopdf.php(73): Imagick->readimagefile(Resource id #4) #1 {main} thrown in /addons/jpgtopdf.php on line 73

    So it seems that the problem is not anymore about the file itself, but the file coding. Any idea? :(
    Thank you very much!

    ReplyDelete
  4. Hi

    Looks like your code is correct, yes. I think you are missing libraries/delegates for Imagick to read either your PDF file or convert it to JPEG files. ImageMagick doesn't always include these libraries by default.

    Try to use different image formats to see if this is the case, e.g. use a GIF file instead of PDF and same with JPEG. Might be clearer if you try to do it in command line using "convert".

    To see if you have the right delegates try:
    "convert -list configure"
    And look for the line starting with DELEGATES. The list should contain 'jpeg' for JPEG and 'gs' for PDF.
    Or "identify -list format" and look for JPEG to listed with 'rw'(read/write).

    ImageMagick delegates can be downloaded from http://www.imagemagick.org/download/delegates/

    If it is PDF decoding that is failing, you might be missing GhostScript. Download it from the delegates page or http://pages.cs.wisc.edu/~ghost/ .

    (I get the following error on my windows machine when I don't have GhostScript installed:
    Magick: Postscript delegate failed `test.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/663.
    Magick: missing an image filename `test.gif' @ error/convert.c/ConvertImageCommand/2974.
    )

    Also have a look at:
    http://blog.ericlamb.net/2008/11/fix-for-convert-no-decode-delegate-for-this-image-format/
    http://old.nabble.com/Imagemagick-and-no-delegates-for-format-%28jpg%29-td14755375.html
    http://www.imagemagick.org/script/advanced-unix-installation.php

    ReplyDelete