Created
November 22, 2016 13:08
-
-
Save hboregio/e6d143149a6bc16ab852edd3401408c5 to your computer and use it in GitHub Desktop.
Bash script to optimize png files
This file contains 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 | |
if [ $# -lt 1 ] | |
then | |
echo "Missing argument" | |
echo "Usage : $0 <APPLICATION_PROJECT_DIRECTORY>" | |
exit 1; | |
fi | |
APPLICATION_PROJECT_DIRECTORY=$1 | |
echo "Optimizing PNGs with pngquant" | |
let "optimizedCount = 0" | |
for png in $(find $APPLICATION_PROJECT_DIRECTORY -iname "*[^9].png") | |
do | |
is_optimized=$(exiftool $png | grep -e "Color Type.*:.*Palette") | |
if [ -z "$is_optimized" ] | |
then | |
already_optimized=$(pngquant $png -ext .png -force 2> last_error) | |
if [ $? -eq 0 ] | |
then | |
echo "Optimized - $png" | |
let "optimizedCount++" | |
else | |
echo "Optimization failed for : $png - Reason : $(cat last_error)" >&2 | |
fi | |
fi | |
done | |
rm last_error 2> /dev/null | |
echo "Optimized $optimizedCount pngs" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment