# IMAGICK WITH HEIC (nginx/php)

This write up will cover building ImageMagick with HEIC support and also building pecl's imagick in order to bring that functionality into PHP for use in web applications. A lot of documentation that has been put out on this topic seems to be exclusive to Ubuntu and/or just totally misses the PHP portion. In addition, the RHEL based world just wants to push everyone to OS version 9. I just can't want to right at this moment. So, this write up will cover that hole.

For the sake of brevity, I have already written a document on installing <span style="color: rgb(52, 73, 94);">**nginx** </span>and <span style="color: rgb(52, 73, 94);">**php**</span>, in addition a document on installing <span style="color: rgb(52, 73, 94);">**libheif**</span>. Please work through those write ups before continuing through on this one. You may already have a loaded web server and if so, just make sure that you cover the **<span style="color: rgb(52, 73, 94);">libheif</span>** install. That install gets the components in place that we will need in order to make this next procedure go smoothly *(libheif holds the keys to heic)*.

Here are links to the previous two documents:  
• [NGINX / PHP / MARIADB INSTALL](https://bs.skullscrape.com/link/35)  
• [LIBHEIF INSTALL](https://bs.skullscrape.com/link/33)

If all is well... here we go.

**PECL**: [https://pecl.php.net](https://pecl.php.net)  
**IMAGEMAGICK**: [https://imagemagick.org](https://imagemagick.org) **(GIT**: [https://github.com/ImageMagick/ImageMagick](https://github.com/ImageMagick/ImageMagick)**)**

```bash
#!/usr/bin/env bash

WRKDIR="/opt"
CODECDIR="/opt/codecs"
IMDIR="/opt/imagick"

# ----- JUMP ON OVER TO OUR HAPPY PLACE -----

cd ${WRKDIR}

# ----- STOP WEB SERVICES -----

systemctl stop nginx php-fpm

# ----- MAKE A DIRECTORY AND PULL DOWN THE SOURCE FILES -----

IMK="3.7.0"
mkdir imagick && cd imagick
wget https://www.imagemagick.org/download/ImageMagick.tar.gz
wget https://pecl.php.net/get/imagick-${IMK}.tgz
```

Ok. I need to interrupt myself for a sec.

If ImageMagick and/or the php-pecl-imagick are installed, they need to <span style="text-decoration: underline;">**NOT**</span> be anymore. Remove them if they are.

```bash
# ----- MOVE OUT OF IMAGICK DIRECTORY -----

cd ~

# ----- DNF REMOVE IMAGEMAGICK AND IMAGICK -----

dnf -y remove *ImageMagick* *imagick*
```

OK. If verified that the system is free and clear of those packages, then, back to the not so regularly scheduled program.

```bash
# ----- UNPACK THE PACKAGES -----

cd ${IMDIR}

tar xzf imagick-${IMK}.tgz
tar xzf ImageMagick.tar.gz
IM7=$(tar -tzf ImageMagick.tar.gz | head -1 | cut -f1 -d"/");

# ----- SOURCE THE CODECS AND LOAD LIBRARIES -----

## NOTE: THE PATHS BELOW ARE TAKEN FROM MY PREVIOUS
##       WRITE UP ON INSTALLING LIBHEIF.

## THIS PART ALONE IS KEY TO THE ENTIRE INSTALL!!

source /root/.bashrc
source /opt/codecs/PKGCONFPATH
ldconfig

# ----- INSTALL SOME LIBRARY/DEV PACKAGES (should have most of them from performing the libheif install)-----

dnf -y install \
fontconfig-devel \
freetype-devel \
graphviz-devel \
djvulibre-devel \
fftw-devel \
jbigkit-devel \
libgs-devel \
libjxl-devel \
liblqr-1 \
libraqm-devel \
LibRaw-devel \
librsvg2-devel \
libwmf-devel \
libzip-devel \
OpenEXR-devel \
OpenEXR-libs \
libtool-ltdl-devel

# ----- CONFIGURE IMAGEMAGICK -----

cd ${IMDIR}/${IM7}

./configure \
  --with-bzlib \
  --with-djvu \
  --with-fftw \
  --with-fontconfig \
  --with-freetype \
  --with-gslib \
  --with-gvc \
  --with-heic \
  --with-jbig \
  --with-jpeg \
  --with-jxl \
  --with-lqr \
  --with-lzma \
  --with-openexr \
  --with-openjp2 \
  --with-pango \
  --with-perl \
  --with-png \
  --with-raqm \
  --with-rsvg \
  --with-tiff \
  --with-uhdr \
  --with-webp \
  --with-wmf \
  --with-xml \
  --with-zip \
  --with-zlib \
  --with-zstd \
  --with-modules \
  --with-utilities \
  --enable-hdri \
  --enable-64bit-channel-masks \
  --enable-year2038 \
  --enable-legacy-support \
  --enable-hugepages

# ----- MAKE SURE THAT HEIC WAS FOUND (AND OTHERS YOU WANT) -----

make -j$(nproc)
make install
```

Now that ImageMagick is compiled up real good and installed. We just need to get the pecl imagick built and in its place.

```bash
# ----- CHANGE TO THE IMAGICK DIRECTORY -----

cd ${IMDIR}/imagick-${IMK}

# ----- WE NEED PHP HEADERS - INSTALL THE DEV PACKAGE -----

dnf -y install php-devel

# ----- PHP'IZE THE CODE -----

phpize

# ----- CONFIGURE, MAKE, AND INSTALL -----

./configure
make -j$(nproc)
make install

# ----- CREATE CONIG TO ENABLE IMAGICK WITHIN php.d AND php-zts.d -----

cat > /etc/php.d/40-imagick.ini << "EOF"
; Enable imagick extension module
extension=imagick.so

; Documentation: http://php.net/imagick

; Don't check builtime and runtime versions of ImageMagick
imagick.skip_version_check=1

; Fixes a drawing bug with locales that use ',' as float separators.
;imagick.locale_fix=0

; Used to enable the image progress monitor.
;imagick.progress_monitor=0

; multi-thread management
;imagick.set_single_thread => 1 => 1
;imagick.shutdown_sleep_count => 10 => 10

; to allow null images
;imagick.allow_zero_dimension_images => 0 => 0
EOF

cat > /etc/php-zts.d/40-imagick.ini << "EOF"
; Enable imagick extension module
extension=imagick.so

; Documentation: http://php.net/imagick

; Don't check builtime and runtime versions of ImageMagick
imagick.skip_version_check=1

; Fixes a drawing bug with locales that use ',' as float separators.
;imagick.locale_fix=0

; Used to enable the image progress monitor.
;imagick.progress_monitor=0

; multi-thread management
;imagick.set_single_thread => 1 => 1
;imagick.shutdown_sleep_count => 10 => 10

; to allow null images
;imagick.allow_zero_dimension_images => 0 => 0
EOF

# ----- COPY MODULE TO PHP-ZTS (so they match) -----

cp -a /usr/lib64/php/modules/imagick.so /usr/lib64/php-zts/modules/

# ----- LOAD LIBS AND RESTART SERVICES -----

ldconfig
systemctl restart nginx php-fpm

exit

```