Solved: PHP Imagick ‘unable to open image’ PDF
October 29, 2015
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
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 (http://stackoverflow.com/questions/6833939/path-environment-variable-for-apache2-on-mac) explaining that the PATH variable is set in the OSX Apache startup
plist file, located at
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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Disabled</key> <true/> <key>Label</key> <string>org.apache.httpd</string> <key>EnvironmentVariables</key> <dict> <key>XPC_SERVICES_UNAVAILABLE</key> <string>1</string> <key>PATH</key> <string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string> </dict> <key>ProgramArguments</key> <array> <string>/usr/sbin/httpd</string> <string>-D</string> <string>FOREGROUND</string> </array> <key>OnDemand</key> <false/> </dict> </plist>
Save, restart apache with
sudo apachectl restart and the problem should be solved.
I hope this helps someone else.