#!/bin/bash
# Build instructions:
# https://medium.com/@eplt/5-minutes-to-install-imagemagick-with-heic-support-on-ubuntu-18-04-digitalocean-fe2d09dcef1
# Command:
# https://zwbetz.com/convert-heic-images-to-jpg/
# The folder with the files you want to convert
directory = /path/to/your/uploads
# Set to 0 to delete the original .heic files after they are converted
keeporiginals = 1
# Set to 0 for PNG (much larger file size)
saveasjpg = 1
# Make sure the mogrify command is available. If this is being
# run as a cron job the which command will fail so we check the
# default location just in case.
which mogrify > /dev/null
if [ [ $? -eq 1 ] ] ; then
if [ [ ! -e "/usr/local/bin/mogrify" ] ] ; then
echo "Unable to run the mogrify command. Please follow the build and installation instructions at the top of the script to install the program on your system."
exit
fi
fi
# Change into the folder with our images
cd " $directory "
# Loop over all the .heic files in the folder
for heic in *.heic; do
# Drop the .heic extension and append .jpg instead
if [ $saveasjpg -eq 0 ] ; then
img = ` echo " $heic " | rev | cut -d. -f2- | rev` .png
else
img = ` echo " $heic " | rev | cut -d. -f2- | rev` .jpg
fi
# If the file we are going to make does not exist, run the command to convert it
if [ $saveasjpg -eq 0 ] ; then
[ -f " $img " ] || /usr/local/bin/mogrify -format png " $heic "
else
[ -f " $img " ] || /usr/local/bin/mogrify -format jpg " $heic "
fi
# If we don't want the originals, give them the axe
[ $keeporiginals -eq 0 ] && rm " $heic "
done