Skip to content

Instantly share code, notes, and snippets.

@btray77
Created July 8, 2016 01:36
Show Gist options
  • Save btray77/4c9adae09bca02a675b84f7d2a99cfb6 to your computer and use it in GitHub Desktop.
Save btray77/4c9adae09bca02a675b84f7d2a99cfb6 to your computer and use it in GitHub Desktop.
Resize images in a folder to a minimum size, keep aspect ratio.
#!/bin/bash
#Edit webroot to meet your needs
IFS=$'\n'
set -e
minimumWidth=1000
minimumHeight=1000
FILES=$(find /webroot/media/catalog/product/ \( -name cache -prune \) -o -name '*' -type f -exec file {} \; | awk -F: '{ if ($2 ~/[Ii]mage|EPS/) print $1}')
AMOUNT=`echo $FILES | wc -w`
COUNTER=0
if [ ! -z "$FILES" ];
then
for F in $FILES
do
imageWidth="$(identify -format "%w" "$F")"
imageHeight="$(identify -format "%h" "$F")"
if [ "$imageWidth" -ge "$minimumWidth" ] || [ "$imageHeight" -ge "$minimumHeight" ]; then
echo "Not Changed. " ''"$imageWidth"x"$imageHeight"'' "$F"
else
#echo "Initial Size"
#ls -lah "$F" | awk -F " " {'print $5'}
mogrify -resize ''"$minimumWidth"x"$minimumHeight<"'' "$F"
#echo "Resized Size"
#ls -lah "$F" | awk -F " " {'print $5'}
let COUNTER=COUNTER+1
NewimageWidth="$(identify -format "%w" "$F")"
NewimageHeight="$(identify -format "%h" "$F")"
echo "Mogrifyed. $NewimageWidth"x"$NewimageHeight"
fi
done
fi
echo "Done! $COUNTER of $AMOUNT files found and changed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment