Skip to content

Instantly share code, notes, and snippets.

@WaYdotNET
Forked from danielantelo/optimize.sh
Created August 31, 2018 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WaYdotNET/8d9d73be2e97b15b628ea61e8f201d6e to your computer and use it in GitHub Desktop.
Save WaYdotNET/8d9d73be2e97b15b628ea61e8f201d6e to your computer and use it in GitHub Desktop.
Optimise images in a folder for web
#!/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