Last active
September 10, 2018 13:44
-
-
Save valentineus/ca02324e302cce72ef5e58a5dd297948 to your computer and use it in GitHub Desktop.
Optimizing images for Google PageSpeed.
This file contains hidden or 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 | |
# Author: Valentin Popov | |
# Email: [email protected] | |
# Date: 2018-09-10 | |
# Usage: /bin/bash image_optimizer.sh /path/to/input | |
# Description: Optimizing images for Google PageSpeed. | |
# Updating the Environment | |
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" | |
export PATH="$PATH:/usr/local/scripts" | |
exec 1> >(logger --stderr --tag IMAGE_OPTIMIZER) 2>&1 | |
echo "Script started" | |
if [ "$1" ]; then | |
INPUT="$(readlink --canonicalize-missing "$1")" | |
if ! [ -d "$INPUT" ]; then | |
echo "Destination directory does not exist" | |
exit 1 | |
fi | |
else | |
echo "No INPUT parameter" | |
exit 1 | |
fi | |
if ! [ -x "$(command -v jpegtran)" ]; then | |
echo "jpegtran is not installed" | |
exit 1 | |
fi | |
if ! [ -x "$(command -v pngquant)" ]; then | |
echo "pngquant is not installed" | |
exit 1 | |
fi | |
echo "Create a backup..." | |
DIRECTORY="$(basename "$INPUT")" | |
tar -cjSf "${DIRECTORY}_backup_$(date "+%G_%m_%d_%H_%M").tar.bz2" "$DIRECTORY" | |
echo "Processing of JPG images..." | |
find "$INPUT" -iname "*.jpg" -type f -print | while read FILE; do | |
echo "In process: $(basename "$FILE")" | |
jpegtran -copy none -optimize -outfile "$FILE" "$FILE" | |
done | |
echo "Processing of PNG images..." | |
find "$INPUT" -iname "*.png" -type f -print | while read FILE; do | |
echo "In process: $(basename "$FILE")" | |
pngquant --force --output "$FILE" --quality 65-80 --speed 1 --strip "$FILE" | |
done | |
# End of work | |
echo "Script has been successfully completed" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment