Last active
May 12, 2021 11:17
-
-
Save danielantelo/52f928d70d4d7da6f53b317780e4afc3 to your computer and use it in GitHub Desktop.
Optimise images in a folder 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 | |
commandExists () { | |
type "$1" &> /dev/null ; | |
} | |
installImageMagick() { | |
if commandExists brew; then | |
brew install imagemagick | |
else | |
apt-get install imagemagick | |
fi | |
} | |
installJpegOptim() { | |
if commandExists brew; then | |
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null | |
brew install jpegoptim | |
else | |
apt-get install jpegoptim | |
fi | |
} | |
installOptiPng() { | |
if commandExists brew; then | |
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null | |
brew install optipng | |
else | |
apt-get install optipng | |
fi | |
} | |
resize() { | |
RESIZE_PARAM='1200x640>' | |
# resize different file types in parallel | |
find . -name "*.jpg" -exec convert -resize $RESIZE_PARAM -verbose {} {} \; & find . -name "*.jpeg" -exec convert -resize $RESIZE_PARAM -verbose {} {} \; & find . -name "*.png" -exec convert -resize $RESIZE_PARAM -verbose {} {} \; | |
} | |
optimizePng() { | |
# change -o5 to the desired levels of optimisation, the higher the number the slower it will go | |
find . -name "*.png" -exec optipng -o5 -strip all {} \; | |
} | |
optimizeJpg() { | |
QUALITY=65 | |
# optimize differen file types in parallel | |
find . -name "*.jpg" -exec jpegoptim --max=$QUALITY -o -p --strip-all {} \; & find . -name "*.jpeg" -exec jpegoptim --max=$QUALITY -o -p --strip-all {} \; | |
} | |
if ! commandExists jpegoptim; then | |
installJpegOptim | |
fi | |
if ! commandExists optipng; then | |
installOptiPng | |
fi | |
resize | |
optimizeJpg & optimizePng |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The resizing appears not to be working (anymore?)