From bcbf28b67b8f5000f5b29523d796b3a15c25fd8a Mon Sep 17 00:00:00 2001 From: Gianmarco Gargiulo Date: Thu, 18 Aug 2022 16:31:06 +0200 Subject: [PATCH] Updated the compile script (v2) with extra flags and image optimization --- compile | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/compile b/compile index 6600334..8b926c7 100755 --- a/compile +++ b/compile @@ -1,7 +1,65 @@ -#/bin/bash +#!/bin/bash -if [[ -d "public" ]]; then - rm -rf public/* && hugo +# gmgsite compile script v2, merged with b-m-f's hugo-picture-optimization + +MAXIMUM_JPG_SIZE=250 +PNG_OPTIMIZATION_LEVEL=2 + +optimize_images () { + +# OG by b-f-m on GitHub + +if hash exiftool 2>/dev/null; then + # remove exif data on all images in new_images + exiftool -all= public/ -r else - mkdir public && hugo + echo "Install perl-image-exiftool to optimize images" fi + +if hash jpegoptim 2>/dev/null; then + for image in $(find public/ -regextype posix-extended -iregex ".*\.(jpeg|jpg)"); do + # resize to width 1400 only if bigger than that + mogrify -resize '1400>' $image + # remove all metadata and try to optimize jpeg image to match the Maximum size defined above + jpegoptim --strip-all --size=$MAXIMUM_JPG_SIZE $image + done; +else + echo "Install jpegoptim to optimize JPEG images" +fi + +if hash optipng 2>/dev/null; then + for image in $(find public/ -regextype posix-extended -iregex ".*\.(png)"); do + # resize to width 1400 only if bigger than that + mogrify -resize '1400>' $image + # optimize PNG with a give level (higher = slower) and remove all metadata + optipng -clobber -strip all -o $PNG_OPTIMIZATION_LEVEL $image + done; +else + echo "Install optipng to optimize PNG images" +fi + +find -type f -name '*_original' -delete + +} + +case $1 in + --no-clean-folder | -c) + mkdir public; hugo && optimize_images;; + + --no-optimize-images | -i) + if [[ -d "public" ]]; then + rm -rf public/* && hugo + else + mkdir public && hugo + fi;; + + --just-compile | -j) + mkdir public; hugo;; + + *) + if [[ -d "public" ]]; then + rm -rf public/* && hugo && optimize_images + else + mkdir public && hugo && optimize_images + fi;; +esac