gmgsite/compile

66 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# 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 -ext jpg -ext jpeg -ext png
else
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