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.
If you’re running PHP 8 see Installing Imagick for PHP 8 on Windows 10.
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.
- 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.
- Determine your version of PHP, whether or not it’s Thread Safe, and the architecture.
- Download the Imagick extension you need.
php.inito enable the
- 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.
Determine your version of PHP
php -i to run
phpinfo() and look at the first few lines of the output
PHP Version => 7.0.7 System => Windows NT QW 10.0 build 10586 (Windows 10) AMD64 Build Date => May 25 2016 12:48:08 Compiler => MSVC14 (Visual C++ 2015) Architecture => x64 ... Loaded Configuration File => C:\php7\php.ini ... Thread Safety => enabled ...
Download the Imagick extension you need
- Visit http://windows.php.net/downloads/pecl/releases/imagick
- Download a version compatible with your version of PHP.
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 Value extension_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.
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
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
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.
php -i and check the output.
imagick imagick module => enabled imagick module version => 3.4.3RC1 imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel Imagick compiled with ImageMagick version => ImageMagick 6.9.3-7 Q16 x64 2016-03-27 http://www.imagemagick.org Imagick using ImageMagick library version => ImageMagick 6.9.3-7 Q16 x64 2016-03-27 http://www.imagemagick.org ImageMagick copyright => Copyright (C) 1999-2015 ImageMagick Studio LLC ImageMagick 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
Install ImageMagick to a directory of your choice
- Extract the whole of the
bindirectory 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
- Add an entry to the
PATHfor ImageMagick ensuring it is before the entry for the PHP extensions.
- Add an entry for
MAGICK_HOMEsetting it to the ImageMagick home directory.
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.
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: => 234 ImageMagick 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
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: => 0 ImageMagick 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\extdirectory resolved this problem.
- Changing the sequence of directories in the
PATHalso resolved it.
MAGICK_HOMEis correctly set in the System environment variables.
Summary of downloads
Note: I chose to update my version of PHP to 7.0.7
|VC14 x64 Thread Safe (2016-May-25 23:02:16)
My local installation
|PHP 7.0.7 thread safe x64
|PHP configuration file
|PHP extension including
|Extracted files from the ImageMagick .zip file’s
|includes C:\ImageMagick-6.9.3-7, C:\php7 and C:\php7\ext in that order