This article was published over a year ago, so please be aware that some content may be out of date.

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

Update: This instructions in this article should no longer be necessary as you can once again install imagick via pecl:

pecl install imagick

If you are still having issues, I would recommend this excellent article by Andrea Olivato.


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
Laravel
Different Ways To Pass Data To A Laravel View
Different Ways To Pass Data To A Laravel View

Laravel views allow you to pass data to a view in a number of different ways. In this article, I'll go over four methods and describe the pros and cons for each one.

Read this article
Tips
Better Http Status Codes In Laravel
Better Http Status Codes In Laravel

This Laravel quick tip will show you how to make your code more readable and expressive by replacing http status code magic numbers with calls to static constants.

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