Install the PHP Imagick extension
The PHP Imagick extension allows PHP code to use the ImageMagick library for image processing.
The Imagick extension supports PHP 5.6 through PHP 8.4. The Imagick extension is not yet supported on PHP 8.5
Install the Imagick Extension
Installing the extension involves the following steps:
- Install build dependencies (packages that are required to build the extension).
- Build the extension’s library file.
- Enable the extension in the PHP configuration.
Install build dependencies
Set environment variables for the package manager.
export DEBIAN_FRONTEND=noninteractiveInstall a compiler and other packages that are required for building PHP extensions.
sudo apt-get -y install gcc g++ make autoconf libc-dev pkg-configInstall additional packages that are required to build the Imagick extension.
sudo apt-get -y install libmagickwand-devBuild the extension
The PECL install command compiles the extension’s .so library file.
yes '' | sudo pecl8.4-sp install imagickyes '' | sudo pecl8.3-sp install imagickyes '' | sudo pecl8.2-sp install imagickyes '' | sudo pecl8.1-sp install imagickyes '' | sudo pecl8.0-sp install imagickyes '' | sudo pecl7.4-sp install imagickyes '' | sudo pecl7.3-sp install imagickyes '' | sudo pecl7.2-sp install imagickyes '' | sudo pecl7.1-sp install imagickyes '' | sudo pecl7.0-sp install imagickyes '' | sudo pecl5.6-sp install imagickEnable the extension
Configure PHP to load the extension.
sudo bash -c "echo extension=imagick.so > /etc/php8.4-sp/conf.d/imagick.ini"sudo bash -c "echo extension=imagick.so > /etc/php8.3-sp/conf.d/imagick.ini"sudo bash -c "echo extension=imagick.so > /etc/php8.2-sp/conf.d/imagick.ini"sudo bash -c "echo extension=imagick.so > /etc/php8.1-sp/conf.d/imagick.ini"sudo bash -c "echo extension=imagick.so > /etc/php8.0-sp/conf.d/imagick.ini"sudo bash -c "echo extension=imagick.so > /etc/php7.4-sp/conf.d/imagick.ini"sudo bash -c "echo extension=imagick.so > /etc/php7.3-sp/conf.d/imagick.ini"sudo bash -c "echo extension=imagick.so > /etc/php7.2-sp/conf.d/imagick.ini"sudo bash -c "echo extension=imagick.so > /etc/php7.1-sp/conf.d/imagick.ini"sudo bash -c "echo extension=imagick.so > /etc/php7.0-sp/conf.d/imagick.ini"sudo bash -c "echo extension=imagick.so > /etc/php5.6-sp/conf.d/imagick.ini"Restart PHP-FPM.
sudo service php8.4-fpm-sp restartsudo service php8.3-fpm-sp restartsudo service php8.2-fpm-sp restartsudo service php8.1-fpm-sp restartsudo service php8.0-fpm-sp restartsudo service php7.4-fpm-sp restartsudo service php7.3-fpm-sp restartsudo service php7.2-fpm-sp restartsudo service php7.1-fpm-sp restartsudo service php7.0-fpm-sp restartsudo service php5.6-fpm-sp restartVerify the extension is enabled in PHP.
php8.4-sp -i | grep imagickphp8.3-sp -i | grep imagickphp8.2-sp -i | grep imagickphp8.1-sp -i | grep imagickphp8.0-sp -i | grep imagickphp7.4-sp -i | grep imagickphp7.3-sp -i | grep imagickphp7.2-sp -i | grep imagickphp7.1-sp -i | grep imagickphp7.0-sp -i | grep imagickphp5.6-sp -i | grep imagickIf the extension is enabled, you will see output like the following.
/etc/phpX.Y-sp/conf.d/imagick.ini,imagickimagick module => enabledimagick module version => 3.4.3RC1imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIteratorImagick compiled with ImageMagick version => ImageMagick 6.6.9-7 2016-06-01 Q16 http://www.imagemagick.orgImagick using ImageMagick library version => ImageMagick 6.6.9-7 2016-06-01 Q16 http://www.imagemagick.orgimagick.locale_fix => 0 => 0imagick.progress_monitor => 0 => 0imagick.skip_version_check => 0 => 0Uninstall the Imagick extension
Uninstalling the extension involves the following steps:
- Disable the extension in the PHP configuration.
- Remove the extension’s library file.
Disable the extension
Remove the extension’s configuration from PHP.
sudo rm /etc/php8.4-sp/conf.d/imagick.inisudo rm /etc/php8.3-sp/conf.d/imagick.inisudo rm /etc/php8.2-sp/conf.d/imagick.inisudo rm /etc/php8.1-sp/conf.d/imagick.inisudo rm /etc/php8.0-sp/conf.d/imagick.inisudo rm /etc/php7.4-sp/conf.d/imagick.inisudo rm /etc/php7.3-sp/conf.d/imagick.inisudo rm /etc/php7.2-sp/conf.d/imagick.inisudo rm /etc/php7.1-sp/conf.d/imagick.inisudo rm /etc/php7.0-sp/conf.d/imagick.inisudo rm /etc/php5.6-sp/conf.d/imagick.iniRestart PHP-FPM.
sudo service php8.4-fpm-sp restartsudo service php8.3-fpm-sp restartsudo service php8.2-fpm-sp restartsudo service php8.1-fpm-sp restartsudo service php8.0-fpm-sp restartsudo service php7.4-fpm-sp restartsudo service php7.3-fpm-sp restartsudo service php7.2-fpm-sp restartsudo service php7.1-fpm-sp restartsudo service php7.0-fpm-sp restartsudo service php5.6-fpm-sp restartRemove the extension
The PECL uninstall command removes the extension’s .so library file.
sudo pecl8.4-sp uninstall imagicksudo pecl8.3-sp uninstall imagicksudo pecl8.2-sp uninstall imagicksudo pecl8.1-sp uninstall imagicksudo pecl8.0-sp uninstall imagicksudo pecl7.4-sp uninstall imagicksudo pecl7.3-sp uninstall imagicksudo pecl7.2-sp uninstall imagicksudo pecl7.1-sp uninstall imagicksudo pecl7.0-sp uninstall imagicksudo pecl5.6-sp uninstall imagickUsing Imagick with PDF files
To allow ImageMagick to process PDF files,
SSH into your server as root and edit the file:
/etc/ImageMagick-6/policy.xmlLocate the following line in the file:
<policy domain="coder" rights="none" pattern="PDF" />;Comment out this line by replacing it with the following:
<!--<policy domain="coder" rights="none" pattern="PDF" />-->;Finally, restart PHP.
sudo service php8.4-fpm-sp restartsudo service php8.3-fpm-sp restartsudo service php8.2-fpm-sp restartsudo service php8.1-fpm-sp restartsudo service php8.0-fpm-sp restartsudo service php7.4-fpm-sp restartsudo service php7.3-fpm-sp restartsudo service php7.2-fpm-sp restartsudo service php7.1-fpm-sp restartsudo service php7.0-fpm-sp restartsudo service php5.6-fpm-sp restart