2022-08-18 16:31:06 +02:00
|
|
|
#!/bin/bash
|
2022-07-14 23:12:56 +02:00
|
|
|
|
2022-08-18 16:31:06 +02:00
|
|
|
# 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
|
|
|
|
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;
|
2022-07-14 23:12:56 +02:00
|
|
|
else
|
2022-08-18 16:31:06 +02:00
|
|
|
echo "Install optipng to optimize PNG images"
|
2022-07-14 23:12:56 +02:00
|
|
|
fi
|
2022-08-18 16:31:06 +02:00
|
|
|
|
|
|
|
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
|