Solved: PHP Imagick ‘unable to open image’ PDF

October 29th, 2015

Warning: This post is 8 years old. Some of this information may be out of date.

I've recently stumbled across an issue where PHP Imagick can't open a PDF file despite the file being there and readable. This is using Mac OSX El Capitan with Homebrewed PHP5, Imagemagick, Ghostscript and the PHP55-imagick module. After a bit of searching I found the fix.

The following code was failing:

    $file = 'test.pdf';
    $im = new Imagick($file);

This was throwing the following error:

    Fatal error: Uncaught Exception 'ImagickException' with message 'unable to open image 'test.pdf': No such file or directory

After a bit of searching I was able to determine that Imagemagick was attempting to use Ghostscript's gs binary, which is found in /usr/local/bin. After checking the Apache variables in the output from <?php phpinfo(); ?> I found that this directory wasn't in Apache's $PATH variable.

I then looked around in /etc/apache2 but couldn't find anywhere to set this. After a bit of searching I found a link on Stack Overflow ( explaining that the PATH variable is set in the OSX Apache startup plist file, located at /System/Library/LaunchDaemons/org.apache.httpd.plist.

The solution is to add a node to the file adding the whole PATH environment variable. Open the /System/Library/LaunchDaemons/org.apache.httpd.plist file and add the path node and value:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
    <plist version="1.0">

Save, restart apache with sudo apachectl restart and the problem should be solved.

I hope this helps someone else.