Configure Imagick PHP Extension in App Service
This blog explains how to configure Imagick PHP Extension in App Service Windows and the alternatives you have for App Service Linux.
Reference: Microsoft Q&A questions.
App Service Windows
Based on your PHP version and Platform (32/64 bits), get the latest php_imagick.dll
from https://windows.php.net/downloads/pecl/releases/imagick/.
Since App Service Windows is running IIS as the webserver, you can select NTS (Non-Thread Safe). This is used when PHP runs on Fast CGI binary.
PHP 7.4
For this scenario php_imagick-3.7.0-7.4-nts-vc15-x64.zip
was selected.
- Go to Kudu site browsing to https://yoursitename.scm.azurewebsites.net/DebugConsole
- Create a folder named
imagick
insidec:\home\site
orD:\home\site
(in any applicable scenario). -
Unzip the folder and copy all
CORE_RL_*
files toc:\home\site\imagick\
ord:\home\site\imagick\
-
Create two new folders
ext
andini
insidec:\home\site
ord:\home\site
(in any applicable scenario). -
Copy
php_imagick.dll
toc:\home\site\ext\
ord:\home\site\ext\
-
Create a new file named
extensions.ini
insidec:\home\site\ini\
ord:\home\site\ini\
with the following content:extension=C:\home\site\ext\php_imagick.dll
-
Create new file
applicationHost.xdt
file inside thec:\home\site
ord:\home\site
folder and copy the below configuration:<?xml version="1.0"?> <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%;c:\home\site\imagick" xdt:Locator="Match(name)" xdt:Transform="InsertIfMissing" /> </environmentVariables> </runtime> </system.webServer> </configuration>
-
Add the following App Settings in Azure Portal:
Note: Check if c or d drives applies to you.
- MAGICK_CODER_MODULE_PATH = c:\home\site\imagick
- MAGICK_HOME = c:\home\site\imagick
- PHP_INI_SCAN_DIR = c:\home\site\ini
- If you create a info page, you should see the following results:
<? phpinfo();
App Service Linux
PHP images are pre-build with imagick extension.
Note: Currently imagick is having compilation issues with PHP 8.x, recommend to use PHP 7.4 for now, for more information check this reference.
ImageMagick policies
ImageMagick best practices strongly encourages you to configure a security policy that suits your local environment. You can read more about these policies here.
Policies can be found on /etc/ImageMagick-6/policy.xml
If you desire to modify these policies on Azure App Service, the best option to accomplish this will be to use a startup script.
Here is an example for removing policies for ghostscript:
#!/bin/bash
#Removing ghostscript policies
sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
apt-get upgrade && apt-get install -y ghostscript
apache2-foreground
Troubleshooting
ghostscript
-
Scenario: Uncaught ImagickException: attempt to perform an operation not allowed by the security policy.
PHP Fatal error: Uncaught ImagickException: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408 in /home/site/wwwroot/index.php:11\nStack trace:\n#0 /home/site/wwwroot/index.php(11): Imagick->readImage('random.pdf')\n#1 {main}\n thrown in /home/site/wwwroot/index.php on line 11
Resolution: Add a custom startup script to modify existing policy with read and/or write or removing the policy, check steps above.
-
Scenario: PHP Fatal error: Uncaught ImagickException: FailedToExecuteCommand ‘gs’
PHP Fatal error: Uncaught ImagickException: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r72x72' -dFirstPage=1 -dLastPage=1 '-sOutputFile=/tmp/magick-60nJzxfIQqHrXx%d' '-f/tmp/magick-60rZK5yQMK31K7' '-f/tmp/magick-60_Dsu584RGIyH'' (1) @ error/pdf.c/InvokePDFDelegate/291
Resolution: Add a custom startup script to modify existing policy with
apt-get upgrade && apt-get install -y ghostscript
, check steps above.