ImageMagick is a command-line tool used for image manipulation tasks such as format conversion, resizing, and transformation. In PHP, the Imagick extension provides a binding to ImageMagick, allowing developers to utilize these features directly in PHP scripts. It supports more formats and advanced features than the default GD library.

The php-imagick package integrates ImageMagick with PHP, enabling image processing in web applications. It simplifies workflows by allowing format conversion, layer manipulation, transparency handling, and effect application within PHP. This ensures efficient image optimization for various purposes.

Installing Imagick on Ubuntu or Debian involves setting up the necessary packages and configuring PHP to load the extension. Proper installation ensures that PHP can process images with ImageMagick without compatibility issues, allowing seamless operation in server-side environments.

Step-by-step video guide:

Steps to install ImageMagick PHP module on Ubuntu or Debian:

  1. Open terminal from the application launcher or via the <ctrl> + <alt> + <t> keyboard shortcut.
  2. Update the apt package list.
    $ sudo apt update
    [sudo] password for user:
    Hit:1 http://jp.archive.ubuntu.com/ubuntu disco InRelease
    Hit:2 http://jp.archive.ubuntu.com/ubuntu disco-updates InRelease
    Hit:3 http://jp.archive.ubuntu.com/ubuntu disco-backports InRelease
    Hit:4 http://jp.archive.ubuntu.com/ubuntu disco-security InRelease
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done

    Updating the package list ensures you are installing the latest available versions of software packages.

  3. Install the php-imagick package.
    $ sudo apt install --assume-yes php-imagick
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following additional packages will be installed:
      fontconfig-config fonts-dejavu-core fonts-droid-fallback fonts-noto-mono ghostscript gsfonts
      imagemagick-6-common libavahi-client3 libavahi-common-data libavahi-common3 libcups2
      libcupsfilters1 libcupsimage2 libfftw3-double3 libfontconfig1 libgomp1 libgs9 libgs9-common
      libijs-0.35 libjbig0 libjbig2dec0 libjpeg-turbo8 libjpeg8 liblcms2-2 liblqr-1-0 libltdl7
      libmagickcore-6.q16-6 libmagickwand-6.q16-6 libpaper-utils libpaper1 libtiff5 libwebp6
      php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-phpdbg php7.2-readline
      poppler-data ttf-dejavu-core
    Suggested packages:
      fonts-noto ghostscript-x cups-common libfftw3-bin libfftw3-dev liblcms2-utils
      libmagickcore-6.q16-6-extra php-pear poppler-utils fonts-japanese-mincho | fonts-ipafont-mincho
      fonts-japanese-gothic | fonts-ipafont-gothic fonts-arphic-ukai fonts-arphic-uming fonts-nanum
    The following NEW packages will be installed:
      fontconfig-config fonts-dejavu-core fonts-droid-fallback fonts-noto-mono ghostscript gsfonts
      imagemagick-6-common libavahi-client3 libavahi-common-data libavahi-common3 libcups2
      libcupsfilters1 libcupsimage2 libfftw3-double3 libfontconfig1 libgomp1 libgs9 libgs9-common
      libijs-0.35 libjbig0 libjbig2dec0 libjpeg-turbo8 libjpeg8 liblcms2-2 liblqr-1-0 libltdl7
      libmagickcore-6.q16-6 libmagickwand-6.q16-6 libpaper-utils libpaper1 libtiff5 libwebp6
      php-common php-imagick php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-phpdbg
      php7.2-readline poppler-data ttf-dejavu-core
    0 upgraded, 42 newly installed, 0 to remove and 2 not upgraded.
    Need to get 23.3 MB of archives.
    ##### snipped
  4. Check if Imagick is loaded in PHP.
    $ php --modules | grep imagick
    imagick

    The above command confirms if the Imagick module is installed and active in PHP.

  5. Restart the web server (for Apache or PHP-FPM, depending on your setup).
    $ sudo systemctl restart apache2

    For Nginx with PHP-FPM, use:

    $ sudo systemctl restart php*-fpm
  6. (Optional) Create a test file to verify the Imagick extension using phpinfo().
    $ sudo nano /var/www/html/info.php
  7. Add the following PHP code.
    <?php
    phpinfo();
    ?>
  8. Access the file via browser.
    http://your-server-ip/info.php
  9. (Optional) Test image processing by creating a PHP script.
    $ sudo nano /var/www/html/test_imagick.php
  10. Add the following PHP code.
    <?php
    $imagick = new Imagick();
    $imagick->readImage('/path/to/sample-image.jpg');
    $imagick->resizeImage(200, 200, Imagick::FILTER_LANCZOS, 1);
    $imagick->writeImage('/path/to/resized-image.jpg');
    echo "Image resized successfully!";
    ?>
  11. Access the script in your browser.
    http://your-server-ip/test_imagick.php

This guide is tested on Ubuntu:

Version Code Name
22.04 LTS Jammy Jellyfish
23.10 Mantic Minotaur
24.04 LTS Noble Numbat
Discuss the article:

Comment anonymously. Login not required.