Skip to content

Instantly share code, notes, and snippets.

@danielstgt
Last active April 19, 2024 11:33
Show Gist options
  • Star 53 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save danielstgt/dc1068e577bbd8b6e9a6050a6db1f9c3 to your computer and use it in GitHub Desktop.
Save danielstgt/dc1068e577bbd8b6e9a6050a6db1f9c3 to your computer and use it in GitHub Desktop.
Install ImageMagick / Imagick 3.4.4 on PHP 7.4 server (Laravel Forge)
#!/bin/bash
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
apt-get install pkg-config libmagickwand-dev -y
cd /tmp
wget https://pecl.php.net/get/imagick-3.4.4.tgz
tar xvzf imagick-3.4.4.tgz
cd imagick-3.4.4
phpize
./configure
make install
rm -rf /tmp/imagick-3.4.4*
echo extension=imagick.so >> /etc/php/7.4/cli/php.ini
echo extension=imagick.so >> /etc/php/7.4/fpm/php.ini
service php7.4-fpm restart
service nginx restart
@danielstgt
Copy link
Author

danielstgt commented Mar 14, 2020

Installation for PHP 7.4

Make sure you have ImageMagick installed (see below)

If you have ImageMagick installed, perform the following two commands:

wget https://gist.githubusercontent.com/danielstgt/dc1068e577bbd8b6e9a6050a6db1f9c3/raw/4687280a25513ce825f3ffcd31661b67f5896850/imagick3.4.4-PHP7.4-forge.sh
sudo bash imagick3.4.4-PHP7.4-forge.sh

Done!

Install ImageMagick (only if not installed already)

sudo apt install imagemagick

You can confirm the installation with:

convert -version

This should show you something like:

Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff wmf x xml zlib

Installation for PHP 7.3

If you're running PHP 7.3, head over to https://gist.github.com/danielstgt/8ca263e924845d308cf862a3edc718eb for the install script.

@dylan7778
Copy link

dylan7778 commented Aug 17, 2020

This is really nice. Could you update this to include support for HEIC images, since that's so common now?

@danielstgt
Copy link
Author

danielstgt commented Aug 17, 2020

@dylan7778

For HEIC support, ImageMagick has to be configured and built with that option. You also have to build a dependency yourself.

Here are the steps that worked for me.

ImageMagick with HEIC support

The dependency libheif-dev (the HEIC format decoder and encoder) has to be purged, since it has to be built with a newer version:

sudo apt-get purge libheif-dev

The h.265 video codec is also a dependency, install libde265-dev:

sudo apt-get install libde265-dev

Now you can build libheif-dev:

cd /tmp
wget https://github.com/strukturag/libheif/releases/download/v1.7.0/libheif-1.7.0.tar.gz
tar -xvf libheif-1.7.0.tar.gz
cd libheif-1.7.0
./autogen.sh
./configure
make
sudo make install

All dependencies should be ready now to download, build and install ImageMagick with HEIC support:

cd /tmp
wget https://www.imagemagick.org/download/ImageMagick.tar.gz
tar xf ImageMagick.tar.gz
cd ImageMagick-7*
./configure --with-heic=yes
make
sudo make install
sudo ldconfig

If you check the ImageMagick version, you should see heic listed:

convert -version
Version: ImageMagick 7.0.10-28 Q16 x86_64 2020-08-17 https://imagemagick.org
Copyright: © 1999-2020 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP(4.5)
Delegates (built-in): bzlib djvu fontconfig freetype heic jbig jng jpeg lcms lqr lzma openexr png tiff wmf x xml zlib

Restart PHP and nginx:

sudo service php7.4-fpm restart
sudo service nginx restart

@SoftCreatR
Copy link

Regarding the ImageMagick installation including HEIC and AVIF support, you may check out my little script: https://github.com/SoftCreatR/imei

@bgengs
Copy link

bgengs commented Nov 5, 2020

Installation for PHP 7.4

Make sure you have ImageMagick installed (see below)

If you have ImageMagick installed, perform the following two commands:

wget https://gist.githubusercontent.com/danielstgt/dc1068e577bbd8b6e9a6050a6db1f9c3/raw/4687280a25513ce825f3ffcd31661b67f5896850/imagick3.4.4-PHP7.4-forge.sh
sudo bash imagick3.4.4-PHP7.4-forge.sh

Done!

Install ImageMagick (only if not installed already)

sudo apt install imagemagick

You can confirm the installation with:

convert -version

This should show you something like:

Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org
Copyright: © 1999-2017 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff wmf x xml zlib

Installation for PHP 7.3

If you're running PHP 7.3, head over to https://gist.github.com/danielstgt/8ca263e924845d308cf862a3edc718eb for the install script.

This is such a time saver, thank you!

@TorchSK
Copy link

TorchSK commented Jan 14, 2021

Hi,
is there some way to support webp using ImageMagick in Laravel?

@danielstgt
Copy link
Author

@TorchSK

Hi,
is there some way to support webp using ImageMagick in Laravel?

Yes, you have to compile ImageMagick with WebP support. Running convert -version will show you all the available formats, make sure it lists WebP when you have compiled your custom version.

@SoftCreatR
Copy link

Hi,
is there some way to support webp using ImageMagick in Laravel?

ImageMagick != Imagick

Imagick supports everything, that ImageMagick supports. So if you compile ImageMagick with WebP support, you can make use of it in PHP/imagick.

@TorchSK
Copy link

TorchSK commented Jan 14, 2021

Thank you for answers, but I am really noob and have no idea how to compile ImageMagick with webp support. All I know that it has to be compiled with libwebp

@SoftCreatR
Copy link

Install libwebp on your server and then, try reinstalling ImageMagick, or compile it yourself. To do so, first uninstall ImageMagick and then, use my tool for example: https://github.com/SoftCreatR/imei

@atadioli
Copy link

Hi, thanks for this guide.
I see this message with the command "php -v":

PHP Warning:  PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/lib/php/20190902/imagick.so (/usr/lib/php/20190902/imagick.so: cannot open shared object file: No such file or directory), /usr/lib/php/20190902/imagick.so.so (/usr/lib/php/20190902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

How to fix it?

@SoftCreatR
Copy link

Make sure to compile imagick with the corresponding phpize version.

@bolechen
Copy link

bolechen commented Feb 9, 2022

I use

sudo apt install php7.4-imagick

@cooperdk
Copy link

cooperdk commented Jun 23, 2022

I use

sudo apt install php7.4-imagick

Uhm... you can only use that if the version of imagick and php7.4-imagick your OS provides supports webp!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment