check_png_optimized.sh 964 B

1234567891011121314151617181920212223242526272829303132
  1. #!/bin/bash -e
  2. # Only warn if decrease is more than 3%
  3. optimization_requirement=3
  4. git ls-files "*.png" | sort -u | (
  5. optimized=1
  6. temp_file=$(mktemp)
  7. echo "Optimizing png files:"
  8. while read file; do
  9. # Does only run a fuzzy check without -o7 -zm1-9 since it would be too slow otherwise
  10. decrease=($(optipng -nc -strip all -out "$temp_file" -clobber "$file" |& \
  11. sed -n 's/.*(\([0-9]\{1,\}\) bytes\? = \([0-9]\{1,\}\)\.[0-9]\{2\}% decrease).*/\1 \2/p'))
  12. if [[ -n "${decrease[*]}" ]]; then
  13. if [ "${decrease[1]}" -ge "$optimization_requirement" ]; then
  14. echo -en "\033[31m"
  15. optimized=0
  16. else
  17. echo -en "\033[32m"
  18. fi
  19. echo -e "Decrease: ${decrease[0]}B ${decrease[1]}%\033[0m $file"
  20. fi
  21. done
  22. rm "$temp_file"
  23. if [ "$optimized" -eq 0 ]; then
  24. echo -e "\033[1;31mWarning: Could optimized png file(s) by more than $optimization_requirement%.\033[0m" \
  25. "Apply 'optipng -o7 -zm1-9 -nc -strip all -clobber <filename>'"
  26. exit 1
  27. fi
  28. )