Skip to content

Instantly share code, notes, and snippets.

@seebz
Created March 7, 2021 13:49
Show Gist options
  • Save seebz/672ba4d4ed91d4acb4274c5d16c50e32 to your computer and use it in GitHub Desktop.
Save seebz/672ba4d4ed91d4acb4274c5d16c50e32 to your computer and use it in GitHub Desktop.
resizer.sh
#!/usr/bin/env bash
MAXWIDTH=2000
MAXHEIGHT=0
usage() {
echo "Usage: resizer FILENAME"
}
filesize() {
FILE="$1"
ls -sh "$FILE" | cut -d" " -f1
}
process() {
FILE="$1"
WIDTH=$(identify -format "%w" "$FILE" 2>/dev/null)
HEIGHT=$(identify -format "%h" "$FILE" 2>/dev/null)
SIZE=$(filesize "$FILE" 2>/dev/null)
# Not an image
if [ -z "$WIDTH" -o -z "$HEIGHT" ]; then
return 1
fi
# Summary
echo "$FILE"
echo -n "$WIDTH"x"$HEIGHT" "$SIZE"
MODIFIED=
# Max Width
if [ "$MAXWIDTH" -gt 0 -a "$WIDTH" -gt "$MAXWIDTH" ]; then
convert "$FILE" -resize "$MAXWIDTH"x "$FILE"
MODIFIED=true
fi
# Max Weight
if [ "$MAXHEIGHT" -gt 0 -a "$HEIGHT" -gt "$MAXHEIGHT" ]; then
convert "$FILE" -resize x"$MAXHEIGHT" "$FILE"
MODIFIED=true
fi
# Changes summary
if [ "$MODIFIED" = true ]; then
WIDTH=$(identify -format "%w" "$FILE")
HEIGHT=$(identify -format "%h" "$FILE")
SIZE=$(filesize "$FILE")
echo -n -e "\t" "-> "
echo -n "$WIDTH"x"$HEIGHT" "$SIZE"
fi
echo
return 0
}
if [ "$#" == "0" ]; then
usage
exit 1
fi
if [ -z "$1" ]; then
usage
exit 1
fi
while (($#)); do
if [ -f "$1" ]; then
process "$1"
elif [ -d "$1" ]; then
find "$1" -type f | while read file; do
process "$file"
done
else
echo "ERROR: $1 is no file or directory."
exit 1
fi
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment