5 minutes to Install Imagemagick with HEIC support on Ubuntu 20.04 (and 18.04) DigitalOcean
(Updated on 20230815, fixed the libheif install issue, tested on latest/current LAMP droplet on 20.04. The auto script attached works on current 20.04 image; follow manual install for 18.04 LTS, also, thanks to feedback from readers, can run on Amazon Linux 2, CentOS 7, Debian 9 and Ansible. I have included snippets at the end of the article for these. It might take a while to compile, if you have a very big droplet with many vCPUs, it can be all done in under 5-mins. Please continue to share in the comments section. Thanks.)
5 mins to setup Imagemagick with HEIC/HEIF support on Ubuntu 20.04. I am writing this because the default official installation is not supporting HEIC yet and it is a cool new format which is has been fully supported in iOS and Mac since late 2017. Native hardware support on iOS devices. The image quality is much better than JPEG at comparable compression rate which means you can get images to your users faster and better quality. Check out this page if you want to know about about the quality difference. https://nokiatech.github.io/heif/comparison.html
My goal is to have it running so I can process uploaded images into cropped resized thumbnails in HEIC using PHP. Just follow the steps below and you will be up and running in a minutes. Should work on other Linux VPS, but I am using DigitalOcean. Just get a basic Ubuntu 20.04 LAMP Droplet from DigitalOcean, or use my link (Discount code for newbies, I get some free referral credits too) to sign up. Then run the following steps. (I will try to explain some of them at high level, but otherwise, just copy and paste into the command line.) Oh, I am a big fan of Brave browser, if you are into blockchain and crypto, should try it, make a little crypto from your regular web browsing.
Step 1 — Follow the steps below to install the latest version of ImageMagick. (At the time of writing, that’s ImageMagick 7.1.0–35.) Part of this will be to build and install the latest version of libde265 and libheif which are needed for compiling ImageMagick with HEIC option.
sed -Ei 's/^# deb-src /deb-src /' /etc/apt/sources.list
apt-get install build-essential autoconf libtool git-core cmake
apt-get build-dep imagemagick libmagickcore-dev libde265 libheif
git clone https://github.com/strukturag/libde265.git
git clone https://github.com/strukturag/libheif.git
cmake --preset=release ..
tar xf ImageMagick.tar.gz
Step 2 — Install PHP Imagick Plugin. I am doing it the lazy way, install the apt-get version, then install 3.5.1 over it, saves me from having to edit the long php.ini file. (You might prefer to do it the proper way.)
apt install php-imagick
tar -xvzf imagick-3.5.1.tgz
cd imagick-3.5.1/### For current LAMP on 20.04 LTS, use php8.2-dev below.
### (Previous LAMP image had php7, use php7.4-dev below.)
### Or just type "php -v" to check your php version
apt install php8.2-dev
### If you are using 18.04 LTS, use php7.2-dev below.
apt install php7.2-devphpize
Step 3— You are done. Now just restart Apache and run a quick test to see that HEIC is listed on the supported formats list.
systemctl restart apache2### Run the following to check if you have HEIC support in your php
php -r 'phpinfo();' | grep HEIC### You should see:
ImageMagick supported formats => 3FR, 3G2, 3GP, A, AAI, AI, ART, ARW, AVI, AVS, B, BGR, BGRA, BGRO, BIE, BMP, BMP2, BMP3, BRF, C, CAL, CALS, CANVAS, CAPTION, CIN, CIP, CLIP, CMYK, CMYKA, CR2, CRW, CUBE, CUR, CUT, DATA, DCM, DCR, DCRAW, DCX, DDS, DFONT, DJVU, DNG, DPX, DXT1, DXT5, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EPT2, EPT3, ERF, EXR, FAX, FILE, FITS, FLV, FRACTAL, FTP, FTS, G, G3, G4, GIF, GIF87, GRADIENT, GRAY, GRAYA, GROUP4, HALD, HDR, HEIC,...
That’s it. You now have a PHP server that can process HEIC images. Just a simple PHP sample code on how to convert your image to HEIC, you will be able to see the filesize and quality difference of the JPEG and HEIC output.
$im = new Imagick();
$im->readImage( __DIR__ . '/tmp/test.jpg' );
$im->cropThumbnailImage( 1280, 1280 );
$im->writeImage( __DIR__ . '/tmp/test_1280.heic' );
$im->destroy();$im = new Imagick();
$im->readImage( __DIR__ . '/tmp/test.jpg' );
$im->cropThumbnailImage( 1280, 1280 );
$im->writeImage( __DIR__ . '/tmp/test_1280.jpg' );
Now that I got you to read all the steps, if you are really lazy and have a fresh installation of the LAMP droplet, you might just want to run the full script with the following one liner. Of course, you are welcome to check it on gist to see what I have done, basically it is the full script above with default “yes” for all apt-get. I have only tested this on the LAMP droplet as of 20220530. Run it and then go and get some coffee. (Yes, I can run the full script in under 5 mins, from start to finish. Try it with the 16 vCPUs droplet, expensive, but it can be done. With 8 vCPUs, you can do it in 6 mins or so.)
I tested it on DigitialOcean, but many readers have since tested it on other platforms. Please read the comments, but here are some of the highlights:
- Amazon Linux 2 : https://github.com/serverlesspub/imagemagick-aws-lambda-2/pull/26
- Ansible : https://github.com/cimon-io/ansible-role-imagemagick/issues/5
- CentOS 7 : (See comments section)
sudo ./configure — disable-sherlock265
// set the PKG_CONFIG_PATH as well
- Debian 9 : (See comments section)
// if qt4-dev missing
sudo apt install libqt4-dev
I keep a gist of my commonly used ubuntu setup commands here. https://gist.github.com/eplt/0ddea14011ec35fbbcac8a193f2e3e02
Other Tips and Tricks:
- Can use the following to check version numbers of key components, sometimes, after apt-get update, you have different versions. Default LAMP image has PHP 8.1, but updated to 8.2 after update.
— Checking Version of LAMP Components
Why do I care so much about HEIC? I use iPhone and I write apps for it. Apple decided to push ahead with using HEIC as default internal image format since iOS 11 in 2017, so we should make the most out of it. Sure, we can just use compatibility mode to go back to JPEG based workflow, but downgrading an image and wasting more storage is what I want to avoid. Also I am confident that Apple didn’t choose HEIC just to annoy everyone without technical benefits. In fact, I am just surprised that after nearly 30 years, GIFs and JPEGs are still the most common image formats even though compression and encoding algorithms have improved a lot since. The industry just can’t get together to push these standards forward quickly. (Kind of like MP3 still being the most popular music format, when AAC is better.) Google been pushing for WebP in Chrome, but just not brave enough to make it the default file format on Android Camera.
Apple didn’t fully explain their decision for adopting HEIC, but my guess is, it is about the HEVC video format as much as the image format. Both can benefit from hardware acceleration to achieve better quality and saving storage space. The 3 seconds of video in a live photo is something that you don’t care much about, but will bring back memories in years to come.
Not saying that I love HEIC, just have to deal with it. My current solution is to keep 2 versions of each image on server, HEIC for all Apple devices and JPEG for the rest. I am aware that HEIC still doesn’t work with Safari as a native format and need something like libheif-js https://strukturag.github.io/libheif/ to render it, but it is hardware accelerated for many iOS apps which is what I provide to my app users. I know WebP is getting better support, but still hard to imagine all Android phones using it as default camera format in 5–10 years.
Feel free to share your comments below. Like most articles I write, these are more like notes-to-self that I keep online and I will update it every now and then. Stay tuned.
A few of you have reached out for help to actually install it for you. Not something that I generally do because you need to give out passwords, etc, and I just don’t want to risk messing up your server. This guide is already simple enough if you have done basic server setup on linux and digitalocean. I have setup a gig on fiverr as a last resort, if you really can’t get it running but want to have a new droplet with it fully installed, try it.
There are a couple of books on ImageMagick (Amazon affiliate links below)