Solved: PHP Imagick ‘unable to open image’ PDF

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:

This was throwing the following error:

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:

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

I hope this helps someone else.