Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.