Skip to content

Instantly share code, notes, and snippets.

@adover
Last active August 10, 2017 01:39
Show Gist options
  • Save adover/202806e39ab82fb76187011c4adec6cc to your computer and use it in GitHub Desktop.
Save adover/202806e39ab82fb76187011c4adec6cc to your computer and use it in GitHub Desktop.
Recursive JPG/PNG Optimisation
#!/usr/bin/env bash
# Recursive image optimisation. Simply call imageoptim.sh <filename>
# Example bash imageoptim.sh /assets
# Requires jpegoptim and optipng (installable on mac using brew)
echo "---------------------------------------"
echo "------- PNG/JPEG File Optimiser -------"
echo "---------------------------------------"
optimise() {
echo "$1 Optimization started... Trawling through all subfolders"
find . -iname "*.$1" -print0 | while read -d $'\0' file;
do
if [ "$1" == "png" ]
then
echo "Hitting PNG Files"
optipng -o7 -preserve "$file"
elif [ "$1" == "jpg" ]
then
echo "Hitting JPG Files"
jpegoptim --max=90 --strip-all --preserve --totals --all-progressive --force "$file"
fi
done
echo "$1 Optimization completed..."
}
cd $1
optimise "jpg"
optimise "png"
cd -
echo "---------------------------------------"
echo "------- Optimisation complete!! -------"
echo "---------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment