Skip to content

Instantly share code, notes, and snippets.

@jeroenvermeulen
Forked from peterjaap/convertImages.sh
Last active September 25, 2020 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jeroenvermeulen/feb819799dea3d74ae9c to your computer and use it in GitHub Desktop.
Save jeroenvermeulen/feb819799dea3d74ae9c to your computer and use it in GitHub Desktop.
Convert images to smaller size and lower quality to reduce file sizes for Magento's original product photos
#!/bin/bash
# convertImages.sh
# Authors: Peter Jaap Blaakmeer (elgentos.nl) & Jeroen Vermeulen (magehost.pro)
# https://gist.github.com/peterjaap/7080989
# https://gist.github.com/jeroenvermeulen/feb819799dea3d74ae9c
NEWQUALITY=90
NEWWIDTH=2000
DIRECTORY=$HOME/httpdocs/media/catalog/product/
/usr/bin/renice +20 $$ >/dev/null
/usr/bin/ionice -c3 -p $$
if [ ! -x /usr/bin/identify ]
then
/bin/echo "ERROR: /usr/bin/identify is not available. You may need to install ImageMagick."
exit 99
fi
if [ ! -x /usr/bin/convert ]
then
/bin/echo "ERROR: /usr/bin/convert is not available. You may need to install ImageMagick."
exit 99
fi
/usr/bin/du -hs $DIRECTORY
while read -d '' -r FILE
do
WIDTH=$( /usr/bin/identify -format '%w' $FILE | /usr/bin/tr -d "\r\n" );
if [ $WIDTH -gt $NEWWIDTH ]; then
SIZEBEFORE=$( /bin/ls -lah $FILE | /usr/bin/awk '{ print $5}' );
/usr/bin/convert $FILE -resize $NEWWIDTH -quality $NEWQUALITY $FILE;
SIZEAFTER=$( /bin/ls -lah $FILE | /usr/bin/awk '{ print $5}' );
echo "$FILE has been converted - from ${SIZEBEFORE} to ${SIZEAFTER}";
fi
done < <( /usr/bin/find $DIRECTORY -type f \( -iname '*.png' -o -iname '*.jpg' -o -iname '*.gif' \) -print0 )
/usr/bin/du -hs $DIRECTORY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment