compile 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/bash
  2. # gmgsite compile script v2, merged with b-m-f's hugo-picture-optimization
  3. MAXIMUM_JPG_SIZE=250
  4. PNG_OPTIMIZATION_LEVEL=2
  5. optimize_images () {
  6. # OG by b-f-m on GitHub
  7. if hash exiftool 2>/dev/null; then
  8. # remove exif data on all images in new_images
  9. exiftool -all= public/ -r
  10. else
  11. echo "Install perl-image-exiftool to optimize images"
  12. fi
  13. if hash jpegoptim 2>/dev/null; then
  14. for image in $(find public/ -regextype posix-extended -iregex ".*\.(jpeg|jpg)"); do
  15. # resize to width 1400 only if bigger than that
  16. mogrify -resize '1400>' $image
  17. # remove all metadata and try to optimize jpeg image to match the Maximum size defined above
  18. jpegoptim --strip-all --size=$MAXIMUM_JPG_SIZE $image
  19. done;
  20. else
  21. echo "Install jpegoptim to optimize JPEG images"
  22. fi
  23. if hash optipng 2>/dev/null; then
  24. for image in $(find public/ -regextype posix-extended -iregex ".*\.(png)"); do
  25. # resize to width 1400 only if bigger than that
  26. mogrify -resize '1400>' $image
  27. # optimize PNG with a give level (higher = slower) and remove all metadata
  28. optipng -clobber -strip all -o $PNG_OPTIMIZATION_LEVEL $image
  29. done;
  30. else
  31. echo "Install optipng to optimize PNG images"
  32. fi
  33. find -type f -name '*_original' -delete
  34. }
  35. case $1 in
  36. --no-clean-folder | -c)
  37. mkdir public; hugo && optimize_images;;
  38. --no-optimize-images | -i)
  39. if [[ -d "public" ]]; then
  40. rm -rf public/* && hugo
  41. else
  42. mkdir public && hugo
  43. fi;;
  44. --just-compile | -j)
  45. mkdir public; hugo;;
  46. *)
  47. if [[ -d "public" ]]; then
  48. rm -rf public/* && hugo && optimize_images
  49. else
  50. mkdir public && hugo && optimize_images
  51. fi;;
  52. esac