ImageMagick is a popular multi-platform image manipulation tool. Web applications often use the library for its high performance with operations against uploaded images, such as resizing and format conversions, for example.
To use the ImageMagick library with PHP applications, such as WordPress, we must first install the library and then it’s corresponding PHP class. This tutorial will show you how to do so on Ubuntu 18.04.
Installing ImageMagick with Apt
ImageMagick version 6.9.2 is available from the default Ubuntu repositories, and it can simply be installed by running the apt install command.
The following command will install the latest version available in the Ubuntu source repositories.
sudo apt install imagemagick
To list all available version from the Ubuntu repositories, use the apt list command with the -a flag.
sudo apt list imagemagick -a
And to install a specific version of a package we specify it with the apt install command. For example, to install version 188.8.131.52 you would run the following command.
sudo apt install imagemagick:184.108.40.206
Installing Imagick PHP Extension
Version 3.4.3 of the Imagick PHP extension is available from the Ubuntu’s repositories. Like ImageMagick, we can simply install by running the apt install command.
sudo apt install php-imagick
If you require a previous version of php-imagick, you can list the version available from the Ubuntu repositories using the apt list command. This would be useful in the event that the latest patch introduces regressions, which is fairly uncommon.
sudo apt list php-magick -a
The -a flag tells apt to list all version of a package available from the repositories. The output will look similar to the following, and at the time of this writing, there was only a single version available.
php-imagick/bionic,now 3.4.3~rc2-2ubuntu4 amd64 [installed]
Restart Apache Web Server
Installing the module alone isn’t enough. In order for any new PHP extension to be used with your web application Apache must be restarted.
sudo systemctl restart apache2
To verify the installation was successful and that the module is enabled properly, we can use php -m from the command line, and grep the results to limit the output to only the line that is important.
Run the following command to verify the installation.
php -m | grep imagick
If the installation was successful, the output of the command will simply show one line, and it will only contain the name of the module imagick.
For a much more detailed verification of whether the PHP module was installed correctly, use the phpinfo() method.
From the command line, run the following command
php -r 'phpinfo();' | grep imagick
Which will output the following information, where the modules status is shown as enabled.
/etc/php/7.2/cli/conf.d/20-imagick.ini, imagick imagick module => enabled imagick module version => 3.4.3RC2 imagick classes => Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel imagick.locale_fix => 0 => 0 imagick.progress_monitor => 0 => 0 imagick.skip_version_check => 1 => 1
Alternatively, by adding the phpinfo() function to a php script, and then accessing the script from a web browser, we are able to see the module is installed and enabled.