build_optimized.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/bash
  2. MAXIMUM_JPG_SIZE=250
  3. PNG_OPTIMIZATION_LEVEL=2
  4. hugo --gc --minify --buildDrafts=false
  5. if hash exiftool 2>/dev/null; then
  6. # remove exif data on all images in new_images
  7. exiftool -r -all= -ext jpg -ext gif -ext png public
  8. else
  9. echo "Install perl-image-exiftool to optimize images"
  10. fi
  11. THREADS=$(nproc)
  12. optimize_jpeg() {
  13. mogrify -resize '1400>' $1
  14. jpegoptim --strip-all --size=$MAXIMUM_JPG_SIZE $1
  15. }
  16. if hash jpegoptim 2>/dev/null; then
  17. (
  18. for image in $(find public -regextype posix-extended -iregex ".*\.(jpeg|jpg)"); do
  19. # Every THREADSth job, stop and wait for everything
  20. # to complete.
  21. if (( i % THREADS == 0 )); then
  22. wait
  23. fi
  24. ((i++))
  25. optimize_jpeg $image &
  26. done;
  27. )
  28. else
  29. echo "Install jpegoptim to optimize JPEG images"
  30. fi
  31. optimize_png() {
  32. mogrify -resize '1400>' $1
  33. optipng -clobber -strip all -o $PNG_OPTIMIZATION_LEVEL $1
  34. }
  35. if hash optipng 2>/dev/null; then
  36. for image in $(find public -regextype posix-extended -iregex ".*\.(png)"); do
  37. # Every THREADSth job, stop and wait for everything
  38. # to complete.
  39. if (( i % THREADS == 0 )); then
  40. wait
  41. fi
  42. ((i++))
  43. optimize_png $image &
  44. done;
  45. else
  46. echo "Install optipng to optimize PNG images"
  47. fi