Skip to content

Instantly share code, notes, and snippets.

@shakna-israel
Created August 11, 2018 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakna-israel/b22760c3bcfdb4e96099716b4e99c081 to your computer and use it in GitHub Desktop.
Save shakna-israel/b22760c3bcfdb4e96099716b4e99c081 to your computer and use it in GitHub Desktop.
crush all the images!
#!/bin/bash
png() {
w=$(mktemp)
pngcrush -brute "$1" "$w"
rm "$1"
mv "$w" "$1"
du -b "$1"
}
jpg() {
jpegoptim "$1"
du -b "$1"
}
gif() {
gifsicle -O "$1" -o "$1"
du -b "$1"
}
if [ -z "$1" ]; then
echo 'Provide a directory to run compression against images.'
exit 1
fi
for f in $(find "$1" -name '*.jpg' -or -name '*.jpeg' -or -name '*.png' -or -name '*.gif'); do
filename=$(basename -- "$f")
extension="${filename##*.}"
if [ "$extension" = 'jpg' ]; then
jpg "$f"
elif [ "$extension" = 'png' ]; then
png "$f"
elif [ "$extension" = 'gif' ]; then
gif "$f"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment