Last active
February 7, 2020 09:53
-
-
Save Soraph/2f7f89e87f52f97caa86b5b3a79c0964 to your computer and use it in GitHub Desktop.
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 | |
#Author: Matteo Canu <soraph88@gmail.com> | |
# Handy script to resize and optimize image size. | |
# Version 0.2.1 | |
# 2020-02-07: Linting and cleanup | |
wkpath= | |
while getopts "p:l:w:h:b:" OPTION | |
do | |
case $OPTION in | |
p) | |
wkpath=$OPTARG | |
;; | |
l) | |
logfile=$OPTARG | |
;; | |
w) | |
maxwidth=$OPTARG | |
;; | |
h) | |
maxheight=$OPTARG | |
;; | |
*) | |
logit "Invalid option specified" | |
;; | |
esac | |
done | |
#Check for a valid working path | |
if [ -z "${wkpath}" ]; then | |
echo "Usage: $0 -p <working directory>" | |
echo "Available commands:" | |
echo " -l Specify a log file to dump the result of the operations" | |
echo " -w Max Width for the images - wider images will be resized maintaing its aspect ratio" | |
echo " -h Max Height for the images - heigher images will be resized maintaing its aspect ratio" | |
echo "" | |
echo "If a log file is specified there will be no output on the screen." | |
exit | |
fi | |
function human_filesize() { | |
awk -v sum="$1" ' BEGIN {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; } ' | |
} | |
#Definition of the log manager | |
function logit () { | |
if [ -z "${logfile}" ]; then | |
#If no output file is specified we output on the screen | |
echo "[$(date +%H:%M:%S)] $*" | |
else | |
#If a log file is specified we either create it or append to it the informations | |
echo "[$(date +%c)] $*" >> "${logfile}" | |
fi | |
} | |
function slogit () { | |
while read -r data; do | |
logit "${data}" | |
done | |
} | |
if [ -z "${maxwidth}" ] && [ -z "${maxheight}" ]; then | |
logit "No resize requested. Jumping to the optimization tasks." | |
else | |
if command -v convert > /dev/null; then | |
logit "Beginning Resize filesize optimization." | |
spazio_iniziale=$(du -s "${wkpath}" | awk -F "\t" {'print $1'}) | |
find "${wkpath}" -type f -iname "*.jpg" -or -iname "*.jpeg" -or -iname "*.png" | while read -r immagine; do | |
colorspace=$(identify -format %[colorspace] "${immagine}") | |
if [ "$maxwidth" ];then #if I set a width | |
if [ ${#colorspace} -gt 0 ]; then | |
convert "${immagine}" -colorspace RGB -resize "$maxwidth"\> -colorspace "${colorspace}" "${immagine}" | |
else | |
convert "${immagine}" -resize "$maxwidth"\> "${immagine}" | |
fi | |
fi | |
if [ "$maxheight" ];then #if I set an height | |
if [ ${#colorspace} -gt 0 ]; then | |
convert "${immagine}" -colorspace RGB -resize x"$maxheight"\> -colorspace "${colorspace}" "${immagine}" | |
else | |
convert "${immagine}" -resize x"$maxwidth"\> "${immagine}" | |
fi | |
fi | |
done | |
spazio_finale=$(du -s "${wkpath}" | awk -F "\t" {'print $1'}) | |
spazio_guadagnato=$(( (spazio_iniziale - spazio_finale) * 1024 )) | |
logit "Space freed: $( human_filesize ${spazio_guadagnato} )" | |
logit "Resize task terminated." | |
else | |
logit "Missing packet convert, skipping resize tasks." | |
fi | |
fi | |
if command -v optipng > /dev/null; then | |
logit "Beginning PNG filesize optimization." | |
spazio_iniziale=$(du -s "${wkpath}" | awk -F "\t" {'print $1'}) | |
find "${wkpath}" -type f -iname "*.png" | while read -r immagine; do | |
if ! (optipng -o1 -simulate "${immagine}" | grep -q "already optimized" ); then | |
optipng -o4 -quiet "${immagine}" | |
fi | |
done | |
spazio_finale=$(du -s "${wkpath}" | awk -F "\t" {'print $1'}) | |
spazio_guadagnato=$(( (spazio_iniziale - spazio_finale) * 1024 )) | |
logit "Space freed: $( human_filesize ${spazio_guadagnato} )" | |
logit "PNG filesize optimization terminated." | |
else | |
logit "Missing packet optipng, skipping PNG filesize optimization." | |
fi | |
if command -v jpegtran > /dev/null; then | |
logit "Beginning JPEG filesize optimization." | |
spazio_iniziale=$(du -s "${wkpath}" | awk -F "\t" {'print $1'}) | |
find "${wkpath}" -type f -iname "*.jpg" -or -iname "*.jpeg" | while read -r immagine; do | |
jpegtran -copy none -progressive -outfile "${immagine}" "${immagine}" | |
done | |
spazio_finale=$(du -s "${wkpath}" | awk -F "\t" {'print $1'}) | |
spazio_guadagnato=$(( (spazio_iniziale - spazio_finale) * 1024 )) | |
logit "Space freed: $( human_filesize ${spazio_guadagnato} )" | |
logit "JPG filesize optimization terminated." | |
else | |
logit "Missing packet jpegtran, skipping JPEG filesize optimization." | |
fi | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment