Monday, December 14, 2015

Setting up ImageMagick for PHP on Azure

Recently I was trying to get PHP+MySQL web application setup on Azure. I was excited to see how Azure would play with non Microsoft platforms. It was quite promising to see that Azure gave me a web applicaiton with PHP out of the box.

Now I wanted to stretch it further by trying to setup ImageMagick extension. Now installing ImageMagick has historically not been the cleanest process. With numerous libraries floating around it has been a less than ideal experience. I did finally get it working though, thanks to the pointer by Brij Raj Singh at StackoverFlow and the article by Mangesh. I am outlining here the steps I followed, tweaking Mangesh's steps a bit to get ImageMagick working on default PHP installation of Azure.

I am assuming in Azure you already have a web application with PHP enabled, and you are using the latest PHP 5.6 from Azure App Settings.


  1. Download the file http://windows.php.net/downloads/pecl/releases/imagick/3.3.0rc2/php_imagick-3.3.0rc2-5.6-nts-vc11-x86.zip and extract it in a temporary folder
  2. From https://www.imagemagick.org/script/binary-releases.php, scroll to the 'Windows Binary Release' section and download the latest file ending with Q16-x86-dll.exe. (as of writing it was  http://www.imagemagick.org/download/binaries/ImageMagick-6.9.2-8-Q16-x86-dll.exe )
  3. Install the above downloaded exe file on your windows personal machine/laptop
  4. Now from within the FTP of your Azure site, create a folder named 'ext' in your root folder. So the path to this folder would be something like /site/ext
  5. Create a folder named 'ini' in your root folder. So the path to this folder would be something like /site/ini
  6. Create a folder named 'imagickwin' in your root folder. So the path to this folder would be something like /site/imagickwin
  7. In the ini folder created above, create a new file named extensions.ini
  8.  The ini file created above should have the following text extension=d:\home\site\ext\php_imagick.dll 
  9. In Azure portalgo to Application Settings for your app resource, and change PHP to the latest version 5.6
  10. In the Application Settings itself, create a key named 'PHP_INI_SCAN_DIR', and provide its value as d:\home\site\ini . This is as described at https://azure.microsoft.com/en-us/documentation/articles/web-sites-php-configure/ site.



  11. Copy all the Core*.dll files from 'C:\Program Files (x86)\ImageMagick-6.9.2-Q16' to the /site/imagickwin folder.
  12. Copy all DLL files from 'C:\Program Files (x86)\ImageMagick-6.9.2-Q16\modules\coders' to the /site/imagickwin folder.
  13. Now go to the folder where you extracted 'php_imagick-3.3.0rc2-5.6-nts-vc11-x86.zip' archive contents. From this, copy all the dll files to /site/ext folder
  14. Next is the most important step - Create a new file namedapplicationHost.xdt directly under the at the site folder level. This file should have the below:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
  <system.webServer> 
    <runtime xdt:Transform="InsertIfMissing"> 
      <environmentVariables xdt:Transform="InsertIfMissing"> 
        <add name="PATH" value="%PATH%d:\home\site\ext\;" xdt:Locator="Match(name)" xdt:Transform="InsertIfMissing" /> 
        <add name="MAGICK_HOME" value="d:\home\site\ext\" xdt:Locator="Match(name)" xdt:Transform="InsertIfMissing" /> 
        <add name="MAGICK_CODER_MODULE_PATH" value="d:\home\site\imagickwin\" xdt:Locator="Match(name)" xdt:Transform="InsertIfMissing" /> 
      </environmentVariables> 
    </runtime> 
  </system.webServer> 
</configuration>

Now, restart your web application, and load phpinfo() to verify that you are seeing something like below:


And you are done!

3 comments:

Eric Schrader said...

Awesome guide, this worked great. The link was broken so I used this one: http://ftp.icm.edu.pl/packages/ImageMagick/binaries/ImageMagick-6.9.2-8-Q16-x86-dll.exe

Saurabh Kumar said...

Thanks @Eric. I have updated te article to give the parent url for the latest ImageMagick release dll.

Arian Kulp said...

Thanks for posting this. Two years later and there's still no nice Azure extension for it.
I tried to follow your steps but Step #14 seems to be missing a critical detail! Can you specify the location?