Install and Configure ImageMagick for PHP with brew package manager

 ← Dev Articles
👍 0
👎 0

This tutorial will show you step-by-step how to use brew package manager to install ImageMagick and then configure it to work with PHP. 

First check if ImageMagick already exists.  Either command below should do that for you.

 brew info imagemagick


If ImageMagick isn't installed you can simply use the command below to install it.

 brew install imagemagick


Now we need to setup PHP to use the ImageMagick extension.  To do so we can use the pecl package manager.


Check if pkg-config is installed.

 brew info pkg-config

 

If pkg-config isn’t installed run the following command.

 

brew install pkg-config

 

Set a few configurations for pecl.  Run the following 3 commands.

 export PKG_CONFIG_PATH="$(brew --prefix imagemagick)/lib/pkgconfig"

 export CPPFLAGS="-I$(brew --prefix imagemagick)/include/ImageMagick-7"

 export CFLAGS="-DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16"


Install the pecl imagick extension.

 pecl install imagick


Restart PHP with brew.

 brew services restart php


Check that gd and imagick extensions are set up properly.

 php -m | grep -E "gd|imagick"


And that’s it!  You may run into an issue with a non-existing imagick extension.  If you have multiple versions of PHP installed and frequently switch between different versions you will need to install the Imagick extension for each PHP version.