How to Install ImageMagick for EA-PHP and ALT-PHP

por | 5 diciembre, 2021

What is ImageMagick?

ImageMagick is software designed to display, convert, and edit raster and vector images. It can be leveraged with PHP by installing the ImageMagick PHP extension on cPanel servers.

Step 1: Install ImageMagick

For both EA-PHP and ALT-PHP, ensure that the ImageMagick software is installed.

CentOS 7 / CloudLinux 7

yum install ImageMagick ImageMagick-devel -y

AlmaLinux / CloudLinux 8 / CentOS 8*

dnf config-manager --set-enabled powertools
dnf install ImageMagick ImageMagick-devel -y

*CentOS 8 will be EOL in DEC 2021 and should not be used in a production environment.

Step 2: Install the ImageMagick PHP Extension

The installation varies based upon which kind of PHP you want to use the extension with. You can use the extension with just EA-PHP or ALT-PHP, or you can use it with both simultaneously.

EA-PHP Extension via SSH or Terminal

Note:

  • All commands provided below need to be executed via SSH or Terminal as the root user.
  1.  Find the binaries for PECL for all versions of PHP that you have installed:find /opt/cpanel/ -iname pecl | grep bin
  2. Run this command for each version of EA-PHP that needs to make use of the extension:/opt/cpanel/ea-php70/root/usr/bin/pecl install imagick
  3.  On CloudLinux servers, CageFS needs to be updated after installing ImageMagick for EA-PHP. This is best done inside of a screen session:screen -S updateCageFS
    cagefsctl -u
  4. If you are using LiteSpeed or the LSAPI handler with mod_lsapi, you may also need to restart detached PHP processes before you can make use of the extension. The following article outlines how this is done:
    Restart LiteSpeed Detached PHP Processes

 

EA-PHP Extension via WHM

  1. Log in to WHM as the root user.
  2. Navigate to Home »Software »Module Installers.
  3. Click on the Manage link to the right of «PHP Pecl».
  4. Select the version of PHP from the drop-down menu, and click Apply.
  5. Under «Install a PHP Pecl», enter «imagick» (without the quotes).
  6. Click «Install Now».
  7. If the server is CloudLinux update the cage by navigating to: Home »Plugins »CageFS User Manager
  8. Then click on the «Update CageFS Skeleton» button on the bottom right
  9. If you are using LiteSpeed or the LSAPI handler with mod_lsapi, you may also need to restart detached PHP processes before you can make use of the extension. The following article outlines how this is done:
    Restart LiteSpeed Detached PHP Processes

 

ALT-PHP Extension

ALT-PHP comes with ImageMagick installed and working by default. These are the steps required to install and enable ALT-PHP for for all users on the server.

  1. If CageFS is not already installed and working, run these commands via SSH as the root user to set it up for all users on the server. If you are not sure if you have already installed and enabled CageFS for the entire server, running these commands will not hurt the server.yum groupinstall alt-php
    yum update cagefs lvemanager
    /usr/sbin/cagefsctl –init
    /usr/sbin/cagefsctl –enable-all
  2. Next list the versions of ALT-PHP that are available for use:/usr/bin/selectorctl –summary
  3. Set the version of ALT-PHP that you desire for the user to use. In this example ALT-PHP 5.1 is being selected:/usr/bin/selectorctl –set-user-current=5.1 –user=cpanelusername
  4. Update the CageFS skeleton in a screen session with the following commands:screen -S updateCageFS
    cagefsctl -u
  5. If you are using LiteSpeed or the LSAPI handler with mod_lsapi, you may also need to restart detached PHP processes before you can make use of the extension. The following article outlines how this is done:
    Restart LiteSpeed Detached PHP Processes

Note:

  • ImageMagick is available for ALT-PHP 5.1 and up

Step 3: Testing ImageMagick

Run these commands as the root user via SSH or Terminal to create a PHP Info file, then visit the script in your web browser to check for the ImageMagick extension. This test is applicable for both EA-PHP and ALT-PHP.

Note:

  • You must replace «cpanelusername» with the username of the cPanel user with which you are testing.
echo "<?php phpinfo();" > /home/cpanelusername/public_html/phpinfo.php
chown cpanelusername:cpanelusername /home/cptest/public_html/phpinfo.php
chmod 0644 /home/cpanelusername/public_html/phpinfo.php
lynx --dump http://domain.tld/phpinfo.php | grep -i "imagick module"

If you need to further verify the actual functionality of the ImageMagick plugin, you also have the optio of creating and testing with the following script as well:

<?php
$image = new Imagick();
$image->newImage(1, 1, new ImagickPixel('#ffffff'));
$image->setImageFormat('png');
$pngData = $image->getImagesBlob();
echo strpos($pngData, "\x89PNG\r\n\x1a\n") === 0 ? 'Ok' : 'Failed';
echo "\n";

You may also be interested in using the following guide to run a more advanced ImageMagick testing script that shows the image types that are supported on your server, and generates an image in multiple common image formats:

How to Show ImageMagick Supported Image Types via PHP and Generate Test Images For Common Image Formats