Updated the compile script (v2) with extra flags and image optimization

pull/3/head
Gianmarco Gargiulo 2022-08-18 16:31:06 +02:00
parent a9fbd27cad
commit bcbf28b67b
1 changed files with 62 additions and 4 deletions

66
compile
View File

@ -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