1
0

image-optimization.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env bash
  2. set -e
  3. OPTIPNG=$(which optipng)
  4. if ! [ -x "$OPTIPNG" ]; then
  5. echo -e "\033[0;31moptipng executable not found, please install\033[0m" >&2
  6. exit 1
  7. fi
  8. JPEGOPTIM=$(which jpegoptim)
  9. if ! [ -x "$JPEGOPTIM" ]; then
  10. echo -e "\033[0;31mjpegoptim executable not found, please install\033[0m" >&2
  11. exit 2
  12. fi
  13. SCOUR=$(which scour)
  14. if ! [ -x "$SCOUR" ]; then
  15. echo -e "\033[0;31mscour executable not found, please install\033[0m" >&2
  16. exit 3
  17. fi
  18. REQUIRED_SCOUR_VERSION="0.38.2"
  19. SCOUR_VERSION=$(scour --version)
  20. if dpkg --compare-versions $SCOUR_VERSION lt $REQUIRED_SCOUR_VERSION; then
  21. echo "scour version $REQUIRED_SCOUR_VERSION or higher is required, found $SCOUR_VERSION" >&2
  22. exit 3
  23. fi
  24. set +e
  25. CHECK_DIR='../'
  26. if [[ -d "$1" ]]; then
  27. CHECK_DIR=$1
  28. fi
  29. function recursive_optimize_images() {
  30. cd "$1" || return
  31. DIR_NAME=${PWD##*/}
  32. if [[ "$DIR_NAME" == "3rdparty" ]]; then
  33. echo -e "\033[0;36mIgnoring 3rdparty for image optimization\033[0m"
  34. return
  35. elif [[ "$DIR_NAME" == "build" ]]; then
  36. echo -e "\033[0;36mIgnoring build for image optimization\033[0m"
  37. return
  38. elif [[ "$DIR_NAME" == "cypress" ]]; then
  39. echo -e "\033[0;36mIgnoring cypress for image optimization\033[0m"
  40. return
  41. elif [[ "$DIR_NAME" == "node_modules" ]]; then
  42. echo -e "\033[0;36mIgnoring node_modules for image optimization\033[0m"
  43. return
  44. elif [[ "$DIR_NAME" == "tests" ]]; then
  45. echo -e "\033[0;36mIgnoring tests for image optimization\033[0m"
  46. return
  47. elif [[ "$DIR_NAME" == "vendor" ]]; then
  48. echo -e "\033[0;36mIgnoring vendor for image optimization\033[0m"
  49. return
  50. elif [[ "$DIR_NAME" == "vendor-bin" ]]; then
  51. echo -e "\033[0;36mIgnoring vendor-bin for image optimization\033[0m"
  52. return
  53. fi
  54. # Optimize all PNGs
  55. for png in *.png
  56. do
  57. [[ -e "$png" ]] || break
  58. $OPTIPNG -o6 -strip all "$png"
  59. done
  60. # Optimize all JPGs
  61. for jpg in *.jpg
  62. do
  63. [[ -e "$jpg" ]] || break
  64. $JPEGOPTIM --strip-all "$jpg"
  65. done
  66. # Optimize all SVGs
  67. for svg in *.svg
  68. do
  69. [[ -e "$svg" ]] || break
  70. if [[ "$svg" == "default-source.svg" ]]; then
  71. echo -e "\033[0;36mIgnoring $svg image optimization\033[0m"
  72. continue
  73. fi
  74. mv $svg $svg.opttmp
  75. $SCOUR --create-groups \
  76. --enable-id-stripping \
  77. --enable-comment-stripping \
  78. --shorten-ids \
  79. --remove-metadata \
  80. --strip-xml-prolog \
  81. --no-line-breaks \
  82. -i $svg.opttmp \
  83. -o $svg
  84. rm $svg.opttmp
  85. done
  86. # Check all subfolders
  87. for dir in */
  88. do
  89. [[ -e "$dir" ]] || break
  90. if [[ -d "$dir" ]]; then
  91. if git check-ignore $dir -q ; then
  92. echo -e "\033[0;36m$dir is not shipped. Ignoring image optimization\033[0m"
  93. continue
  94. fi
  95. recursive_optimize_images "$dir"
  96. cd ..
  97. fi
  98. done
  99. }
  100. recursive_optimize_images "$CHECK_DIR"