Created
August 11, 2018 04:43
-
-
Save shakna-israel/b22760c3bcfdb4e96099716b4e99c081 to your computer and use it in GitHub Desktop.
crush all the images!
This file contains hidden or 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 | |
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