-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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