Tuesday, February 23, 2021 6:00 AM

How To Build The Imagick Php Extension From Source

This article describes the process you can follow to build the Imagick PHP extension from source, which can be handy if the version you are after has not been released via PECL.

PHP
How To Build The Imagick Php Extension From Source

At the time of writing, the PHP8 compatible version of the Imagick extension has not yet been released, which means it is not available via PECL.

After a quick Google search, I found this excellent article by Freek Van der Herten explaining how to build the php extension from the master branch, which already seems to work with PHP 8.

After I followed Freek's instructions, I noticed that the extention was missing a package version number that is ordinarily populated automatically when the extension is built via the PECL installer. As it turns out, one of my sites also relies on Laravel DOMPDF which requires the Imagick version number to be present for it to work correctly.

Let's go over the steps described by Freek with some additional context and code snippets.

  1. Clone the repository to your local development directory:
git clone https://github.com/Imagick/imagick.git
  1. Open the php_imagick.h located in the root of the imagick directory. Locate the following line:
#define PHP_IMAGICK_VERSION    "@PACKAGE_VERSION@"

and replace "@PACKAGE_VERSION@" with the latest released version. In my case this was 3.4.4, so I modified the line to this:

#define PHP_IMAGICK_VERSION    "3.4.4"
  1. Follow the php.net guide to compile the extension.
# Navigate to the cloned directory
cd /path/to/imagick

# Prepare the build environment for a PHP extension.
phpize

# Run a script to make sure your system has the proper dependencies to compile from source.
./configure

# Compile the extension.
make
  1. Locate and copy the newly created imagick.so extension which can be found in the modules directory inside the cloned imagick directory:
/path/to/imagick/modules/imagick.so
  1. Use the following command to find the directory in which your PHP version expects to find extensions:
php -i | grep extension_dir

php -i prints all php information. This output is piped (sent) to the grep command which searches the output for the given keyword - extension_dir in this case.

This command should return something like this:

extension_dir => /usr/local/lib/php/pecl/20200930 => /usr/local/lib/php/pecl/20200930

Copy the imagick.so file to this directory.

  1. Use the php --ini command to find the loaded configuration file for your php installation. In my case, this was:
/usr/local/etc/php/8.0/php.ini

Open this file in your preferred code editor and add the following line anywhere in the file:

extension="imagick.so"

This will instruct php to load the imagick extension.

All being well, the Imagick extension should now be installed. You might need to restart the PHP service for this change to be registered. In my case, as a Laravel Valet user, I restarted Valet with the following command:

valet restart

Finally, you can use the following command to make sure the extension is loaded correctly:

php -i | grep imagick

This should result in the following output:

imagick module => enabled

I hope this works for you as it did for me. Once again, my thanks go to Freek for most of these instructions and to Danack, the maintaner of the PHP Imagick extension. Please consider sponsoring him if your work depends on the Imagick extension.

Thank you for reading this article.
I hope you learned something useful.
If you've made it this far, you might like to follow me on Twitter where I post similar content and connect with like-minded people.
Follow me on Twitter
PHP
Tidying Tips
Tidying Tips

In a post called The Life Changing Magic of Tidying Up Code, Kent Beck outlines his philosophy on cleaning up code. The article results in a short list of easy-to-follow rules, which I'm sure he would agree are there to be broken.

Read this article
Laravel
Reset Your Laravel App In Seconds
Reset Your Laravel App In Seconds

In his recent talk about Laravel Nova at Laracon US, Taylor Otwell used a nice little shortcut to reset his demo app during the presentation.

Read this article
Privacy Policy
Copyright © 2021 Carl Cassar. All Rights Reserved.