Last active
December 11, 2023 11:40
-
-
Save pduchnovsky/9544ea3447b48c65c2412f7d5aef1f7a to your computer and use it in GitHub Desktop.
Image size optimization for webpages jpg/png | https://duchnovsky.com/2020/11/images-optimization-for-web/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script adapted by pduchnovsky | |
# https://duchnovsky.com/2020/11/images-optimization-for-web/ | |
# Add list of folder names to ignore array here, separated by space | |
ignore=("./themes/*" "./resources/*" "./assets/*") | |
# Array of png optimizers with their arguments | |
optimizers=( | |
"optipng -nb -nc" | |
"advpng -z4" | |
"pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time -ow" | |
) | |
# File to check/create in order to stop processing already processed files since last run. | |
file=optimg.flag | |
# Only use -newer option if file already exists | |
if [ -f "$file" ]; then | |
option="-newer $file" | |
fi | |
# Prepare arguments from ignore list | |
ignorearg=() | |
for ignored in "${ignore[@]}"; do | |
ignorearg+="-not -path "*${ignored}" " | |
done | |
# Optimize png images with optimizers and their settings | |
for optimizer in "${optimizers[@]}" | |
do | |
find . -type f $option -iname "*.png" $ignorearg -exec $optimizer {} \; | |
done | |
# Optimize using jpegoptim | |
find . -type f $option -iregex .*\.jpe?g$ $ignorearg\ | |
-exec jpegoptim -m85 --all-progressive -f --strip-all {} \; | |
# touch file so it updates last run-time | |
touch $file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment