image-optimization.sh 1005 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env bash
  2. CHECK_DIR='../'
  3. if [[ -d "$1" ]]; then
  4. CHECK_DIR=$1
  5. fi
  6. function recursive_optimize_images() {
  7. cd "$1" || return
  8. DIR_NAME=${PWD##*/}
  9. if [[ "$DIR_NAME" == "node_modules" ]]; then
  10. return
  11. elif [[ "$DIR_NAME" == "tests" ]]; then
  12. return
  13. fi
  14. # Optimize all PNGs
  15. for png in *.png
  16. do
  17. [[ -e "$png" ]] || break
  18. optipng -o6 -strip all "$png"
  19. done
  20. # Optimize all JPGs
  21. for jpg in *.jpg
  22. do
  23. [[ -e "$jpg" ]] || break
  24. jpegoptim --strip-all "$jpg"
  25. done
  26. # Optimize all SVGs
  27. for svg in *.svg
  28. do
  29. [[ -e "$svg" ]] || break
  30. mv $svg $svg.opttmp
  31. scour --create-groups \
  32. --enable-id-stripping \
  33. --enable-comment-stripping \
  34. --shorten-ids \
  35. --remove-metadata \
  36. --strip-xml-prolog \
  37. --no-line-breaks \
  38. -i $svg.opttmp \
  39. -o $svg
  40. rm $svg.opttmp
  41. done
  42. # Check all subfolders
  43. for dir in */
  44. do
  45. [[ -e "$dir" ]] || break
  46. if [[ -d "$dir" ]]; then
  47. recursive_optimize_images "$dir"
  48. cd ..
  49. fi
  50. done
  51. }
  52. recursive_optimize_images "$CHECK_DIR"