1
0

image-optimization.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env bash
  2. set -e
  3. OPTIPNG=$(which optipng)
  4. if ! [ -x "$OPTIPNG" ]; then
  5. echo "optipng executable not found, please install" >&2
  6. exit 1
  7. fi
  8. JPEGOPTIM=$(which jpegoptim)
  9. if ! [ -x "$JPEGOPTIM" ]; then
  10. echo "jpegoptim executable not found, please install" >&2
  11. exit 2
  12. fi
  13. SCOUR=$(which scour)
  14. if ! [ -x "$SCOUR" ]; then
  15. echo "scour executable not found, please install" >&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 "Ignoring 3rdparty for image optimization"
  34. return
  35. elif [[ "$DIR_NAME" == "build" ]]; then
  36. echo "Ignoring build for image optimization"
  37. return
  38. elif [[ "$DIR_NAME" == "cypress" ]]; then
  39. echo "Ignoring cypress for image optimization"
  40. return
  41. elif [[ "$DIR_NAME" == "node_modules" ]]; then
  42. echo "Ignoring node_modules for image optimization"
  43. return
  44. elif [[ "$DIR_NAME" == "tests" ]]; then
  45. echo "Ignoring tests for image optimization"
  46. return
  47. elif [[ "$DIR_NAME" == "vendor" ]]; then
  48. echo "Ignoring vendor for image optimization"
  49. return
  50. elif [[ "$DIR_NAME" == "vendor-bin" ]]; then
  51. echo "Ignoring vendor-bin for image optimization"
  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. mv $svg $svg.opttmp
  71. $SCOUR --create-groups \
  72. --enable-id-stripping \
  73. --enable-comment-stripping \
  74. --shorten-ids \
  75. --remove-metadata \
  76. --strip-xml-prolog \
  77. --no-line-breaks \
  78. -i $svg.opttmp \
  79. -o $svg
  80. rm $svg.opttmp
  81. done
  82. # Check all subfolders
  83. for dir in */
  84. do
  85. [[ -e "$dir" ]] || break
  86. if [[ -d "$dir" ]]; then
  87. if git check-ignore $dir -q ; then
  88. echo "$dir is not shipped. Ignoring image optimization"
  89. continue
  90. fi
  91. recursive_optimize_images "$dir"
  92. cd ..
  93. fi
  94. done
  95. }
  96. recursive_optimize_images "$CHECK_DIR"