image-optimization.sh 2.8 KB

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