If you’re running PHP 7 on Windows and you want to use the Imagick PHP extension then you may want to refer to this post for some download and installation instructions.
Background
I recently spent far too long trying to convince incompatible binaries that they should work together. All I wanted to do was to write some code that would automatically correct the orientation of an uploaded image, based on the value of the Orientation field in the image’s EXIF data. And, in order to test it, I needed a method of setting the orientation.
To do that I needed to install the Imagick PHP Extension. I thought that the installation would be doddle. It wasn’t.
I chased a lot of wild geese and followed many red herrings until I eventually found the correct hint for the location of the ImageMagick binary files upon which the Imagick PHP extension is dependent.
I hope that you’ll find these notes to be useful.
Overview
- Imagick is a native PHP extension to create and modify images using the ImageMagick API.
- It provides a DLL that you need to install as an extension to your PHP installation.
- The DLL is dependent on functionality provided by ImageMagick.
- You need to use compatible versions. i.e. The version used to build the library should match the run time version.
- If you don’t then it won’t work.
- Finding the correct files to download was probably the hardest part of the installation.
- The second hardest part was undoing all the failed attempts to configure incompatible versions.
- Then there was making sure these instructions actually worked.
Installation
- Determine your version of PHP, whether or not it’s Thread Safe, and the architecture.
- Download the Imagick extension you need.
- Change
php.ini
to enable the php_imagick
extension. - Extract the .dll files from the Imagick extension zip file.
- Download the compatible version of ImageMagick.
- Install ImageMagick to a directory of your choice.
- Set the environment to enable the ImageMagick DLLs to be found.
- Reboot.
- Test.
Determine your version of PHP
Use php -i
to run phpinfo()
and look at the first few lines of the output
PHP Version => 7.0.7System => Windows NT QW 10.0 build 10586 (Windows 10) AMD64Build Date => May 25 2016 12:48:08Compiler => MSVC14 (Visual C++ 2015)Architecture => x64...Loaded Configuration File => C:\php7\php.ini...Thread Safety => enabled...
Download the Imagick extension you need
Change php.ini to enable the php_imagick extension
Determine the directory for extensions by checking for the extension_dir
directive in your phpinfo output.
Directive => Local Value => Master Valueextension_dir => C:\php7\ext => C:\php\ext
If the top level directory doesn’t match that from which PHP is run then you should either correct the local value or fully qualify the file name of the extension.
extension=c:/php7/ext/php_imagick.dll
Or do both.
Extract the .dlls from the Imagick extension .zip file
Since PHP extensions are loaded dynamically, you need to tell PHP where to find them. It doesn’t use the PATH
for this. You either need to fully qualify the file name or define the extension_dir
directive.
For php_imagick.dll
to be loadable all the DLLs upon which it is statically dependent also need to be accessible.
When ImageMagick is installed then you will be using its DLLs. In the mean time you need to extract all of them, ensuring that they are in a directory that’s in your PATH.
You have two options. Copy the files into a directory that’s already in your PATH, or add the directory to your PATH.
I chose to install all the .dll files ( php_imagick.dll and 7 starting CORE_ ) into c:\php7\ext
and added that directory to my PATH, after the entry for C:\php7
.
Download the compatible version of ImageMagick
With the extension installed it is now possible to determine the version of ImageMagick upon which php_imagick.dll
is dependent.
Run php -i
and check the output.
imagickimagick module => enabledimagick module version => 3.4.3RC1imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernelImagick compiled with ImageMagick version => ImageMagick 6.9.3-7 Q16 x64 2016-03-27 http://www.imagemagick.orgImagick using ImageMagick library version => ImageMagick 6.9.3-7 Q16 x64 2016-03-27 http://www.imagemagick.orgImageMagick copyright => Copyright (C) 1999-2015 ImageMagick Studio LLCImageMagick release date => 2016-03-27
The version of ImageMagick you will need is shown in “Imagick compiled with ImageMagick version”.
Now you won’t find what you’re looking for at www.imagemagick.org. You need to go to http://windows.php.net/downloads/pecl/deps
The file I downloaded was ImageMagick-6.9.3-7-vc14-x64.zip
Install ImageMagick to a directory of your choice
- Extract the whole of the
bin
directory from the ImageMagick zip file to a directory of your choice. I chose C:\ImageMagick-6.9.3-7
Set the environment to enable the ImageMagick DLLs to be found
Use “Edit the system environment variables” to edit the System variables section.
- From the command prompt run
SystemPropertiesAdvanced
. - Add an entry to the
PATH
for ImageMagick ensuring it is before the entry for the PHP extensions. - Add an entry for
MAGICK_HOME
setting it to the ImageMagick home directory.
Reboot
Rebooting is the simplest way of ensuring that your system notices the changes you have made.
If you’re just running PHP from the Command Prompt you should be able to get away with closing one and opening a new Command Prompt.
Or you may choose to use ApacheMonitor to stop and restart your web server. But this may not pick up system environment changes.
Test
- Check your phpinfo() output shows a non-zero value for
ImageMagick number of supported formats
.
You should see something like this ImageMagick number of supported formats: => 234ImageMagick supported formats => a, long, list, of,formats, comma, space, separated, including, common, ones, such, as, JPG, PDF, PNG
- Also test phpinfo from your browser. Expect the same result.
- Try some code.
Basic problem determination
If you get the following when running php -i
Warning: PHP Startup: Unable to load dynamic library 'c:/php7/ext/php_imagick.dll' - The specified module could not be found. in Unknown on line 0
then you will need to make sure the dependent DLLs are correctly installed.
In phpinfo output, if you do not have the correct version of ImageMagick installed and configured then you will probably see
ImageMagick number of supported formats: => 0ImageMagick supported formats =>
If you try running PHP code that uses Imagick and you get CLI has stopped working
, then you have incompatible versions. Check you don’t have another version of Imagick running.
I got this problem when I was running the latest version ( ImageMagick 7.0.2-0 ) from imagemagick.org.
It’s also possible that php -i
produces the correct output but phpinfo on the browser shows 0 supported formats. Possible solutions:
- I discovered that removing the additional DLLs extracted from the php_imagick zip file into the
C:\php7\ext
directory resolved this problem. - Changing the sequence of directories in the
PATH
also resolved it. - Ensuring
MAGICK_HOME
is correctly set in the System environment variables.
Summary of downloads
Note: I chose to update my version of PHP to 7.0.7
My local installation
Directory/File | Contains |
---|
C:\php7 | PHP 7.0.7 thread safe X64 |
C:\php7\php.ini | PHP configuration file |
C:\php7\ext | PHP extensions including php_imagick.dll |
C:\ImageMagick-6.9.3.7 | Extracted files from the ImageMagick .zip file’s bin directory |
Environment variables
Variable | Purpose | Contents |
---|
PATH | System path | includes C:\ImageMagick-6.9.3-7 C:\php7 and C:\php7\ext in that order |
MAGICK_HOME | ImageMagick home | C:\ImageMagick-6.9.3-7 |
References