Last active
March 3, 2017 08:34
-
-
Save blade1989/602ed929051deff5144d6fd54b766520 to your computer and use it in GitHub Desktop.
Images optimizing script
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 : Imri Paloja | |
# Email : imri.paloja@gmail.com | |
# HomePage : https://www.eurobytes.nl/ | |
# Version : 0.6.2 | |
# Name : image_optimizer.sh | |
# Description : Image optimizing script | |
# -------------------------------------------------- | |
# This, is beta. | |
# Still need to make a pretty log, for now it outputs everything in /var/log/image_optimizer.log. | |
# Dependencies | |
# - jpegoptim | |
# - optipng | |
# - exiftool | |
# What this script does: | |
# Finds all the .png and .jpg in a folders, get the width and height. | |
# resizes the images to the known screen width: 1920 1280 980 800 768 360 320 | |
# With respect the aspect ratio. If the image is called "cheese.png" and the width is 1810, it will resize that image to 1280 980 800 768 360 320 160 | |
# meaning the image will look like this: | |
# cheese-optimizer-1280.png | |
# cheese-optimizer-980.png | |
# cheese-optimizer-800.png | |
# cheese-optimizer-768.png | |
# cheese-optimizer-360.png | |
# cheese-optimizer-320.png | |
# cheese-optimizer-160.png | |
# And the original will be preserved ad a 'backup' | |
# cheese.png | |
# After resizing, the resized image, with the prefix "-optimizer-" will be optimized, and stripped of all meta-data | |
# The images will be outputted in the same directory where the original resides | |
# Marking the start time | |
start=`date +%s` | |
# fill this out before runing this script. | |
imagedir="/path/to/images/folder/" | |
find $imagedir/ -type f -iname "*.jpg" -o -iname "*.png" -not -iname "*-optimizer-*" -not -wholename "*/banner/*" | while read imagefile; do | |
# /path/to/images/folder/05-JPcmMgS.png | |
# 05-JPcmMgS.png | |
image="$(basename "$imagefile")" | |
# 05-JPcmMgS-small.png | |
#smallimage="$(basename "$imagefile" | sed "s/\(.*\)\./\1-optimizer-$(echo $width | sed 's/x//g')./g" | awk '{print $1}')" | |
# /path/to/images/folder/ | |
path=$(echo "$imagefile" | sed "s/\/$image//g") | |
# The Current image file size | |
imagesize=$(du -bs "$imagefile" | awk '{print $1}') | |
imagewidth=$(exiftool "$imagefile" | grep "Image Width" | awk '{print $4}') | |
imageheight=$(exiftool "$imagefile" | grep "Image Height" | awk '{print $4}') | |
# Known screen sizes: | |
# image width: 1920 1280 980 800 768 360 320 | |
# image height:1200 900 600 1280 1280 1024 640 480 | |
# 1920 1280 980 800 768 360 320 | |
case $imagewidth in | |
1[3-9][0-9][0-9]) | |
echo "1[3-9][0-9][0-9] - $imagewidth" | |
echo "1280x600 980x1280 800x1280 768x1024 360x640 320x480 160x240" | sed 's/ /\n/g' | while read divide; do | |
# 1280x600 | |
# width="$(($imagewidth / $divide))x" | |
# height="$(($imageheight / $divide))" | |
width="$(($(echo $divide | sed 's/x/\n/g' | head -n 1)))x" | |
height="$(($(echo $divide | sed 's/x/\n/g' | tail -n 1)))" | |
smallimage="$(basename "$imagefile" | sed "s/\(.*\)\./\1-optimizer-$(echo $width | sed 's/x//g')./g" | awk '{print $1}')" | |
convert "$imagefile" -resize $width$height "$path/$smallimage" &>>/var/log/image_optimizer.log | |
done; | |
;; | |
1[0-3][0-9][0-9]) | |
echo "1[0-3][0-9][0-9]) - $imagewidth" | |
echo "980x1280 800x1280 768x1024 360x640 320x480 160x240" | sed 's/ /\n/g'| while read divide; do | |
width="$(($(echo $divide | sed 's/x/\n/g' | head -n 1)))x" | |
height="$(($(echo $divide | sed 's/x/\n/g' | tail -n 1)))" | |
smallimage="$(basename "$imagefile" | sed "s/\(.*\)\./\1-optimizer-$(echo $width | sed 's/x//g')./g" | awk '{print $1}')" | |
convert "$imagefile" -resize $width$height "$path/$smallimage" &>>/var/log/image_optimizer.log | |
done; | |
;; | |
9[0-9][0-9]) | |
echo "9[0-9][0-9] - $imagewidth" | |
echo "800x1280 768x1024 360x640 320x480 160x240" | sed 's/ /\n/g'| while read divide; do | |
width="$(($(echo $divide | sed 's/x/\n/g' | head -n 1)))x" | |
height="$(($(echo $divide | sed 's/x/\n/g' | tail -n 1)))" | |
smallimage="$(basename "$imagefile" | sed "s/\(.*\)\./\1-optimizer-$(echo $width | sed 's/x//g')./g" | awk '{print $1}')" | |
convert "$imagefile" -resize $width$height "$path/$smallimage" &>>/var/log/image_optimizer.log | |
done; | |
;; | |
8[0-9][0-9]) | |
echo "8[0-9][0-9] - $imagewidth" | |
echo "768x1024 360x640 320x480 160x240" | sed 's/ /\n/g'| while read divide; do | |
width="$(($(echo $divide | sed 's/x/\n/g' | head -n 1)))x" | |
height="$(($(echo $divide | sed 's/x/\n/g' | tail -n 1)))" | |
smallimage="$(basename "$imagefile" | sed "s/\(.*\)\./\1-optimizer-$(echo $width | sed 's/x//g')./g" | awk '{print $1}')" | |
convert "$imagefile" -resize $width$height "$path/$smallimage" &>>/var/log/image_optimizer.log | |
done; | |
;; | |
7[0-9][0-9]) | |
echo "7[0-9][0-9] - $imagewidth" | |
echo "360x640 320x480 160x240" | sed 's/ /\n/g'| while read divide; do | |
width="$(($(echo $divide | sed 's/x/\n/g' | head -n 1)))x" | |
height="$(($(echo $divide | sed 's/x/\n/g' | tail -n 1)))" | |
smallimage="$(basename "$imagefile" | sed "s/\(.*\)\./\1-optimizer-$(echo $width | sed 's/x//g')./g" | awk '{print $1}')" | |
convert "$imagefile" -resize $width$height "$path/$smallimage" &>>/var/log/image_optimizer.log | |
done; | |
;; | |
3[0-9][0-9]) | |
echo "3[0-9][0-9] - $imagewidth" | |
echo "320x480 160x240" | sed 's/ /\n/g'| while read divide; do | |
width="$(($(echo $divide | sed 's/x/\n/g' | head -n 1)))x" | |
height="$(($(echo $divide | sed 's/x/\n/g' | tail -n 1)))" | |
smallimage="$(basename "$imagefile" | sed "s/\(.*\)\./\1-optimizer-$(echo $width | sed 's/x//g')./g" | awk '{print $1}')" | |
convert "$imagefile" -resize $width$height "$path/$smallimage" &>>/var/log/image_optimizer.log | |
done; | |
;; | |
3[0-9][0-9]) | |
echo "3[0-9][0-9] - $imagewidth" | |
echo "160x240" | sed 's/ /\n/g'| while read divide; do | |
width="$(($(echo $divide | sed 's/x/\n/g' | head -n 1)))x" | |
height="$(($(echo $divide | sed 's/x/\n/g' | tail -n 1)))" | |
smallimage="$(basename "$imagefile" | sed "s/\(.*\)\./\1-optimizer-$(echo $width | sed 's/x//g')./g" | awk '{print $1}')" | |
convert "$imagefile" -resize $width$height "$path/$smallimage" &>>/var/log/image_optimizer.log | |
done; | |
;; | |
*) | |
echo "Size is off: $imagewidth" | |
# is greater than or equal to: -ge | |
# is less than or equal to: -le | |
# 1920 1280 980 800 768 360 320 | |
if [ "$imagewidth" -gt "2000" ]; then | |
echo "Greater then 2000" | |
echo "1280x600 980x1280 800x1280 768x1024 360x640 320x480 160x240" | sed 's/ /\n/g'| while read divide; do | |
width="$(($(echo $divide | sed 's/x/\n/g' | head -n 1)))x" | |
height="$(($(echo $divide | sed 's/x/\n/g' | tail -n 1)))" | |
smallimage="$(basename "$imagefile" | sed "s/\(.*\)\./\1-optimizer-$(echo $width | sed 's/x//g')./g" | awk '{print $1}')" | |
convert "$imagefile" -resize $width$height "$path/$smallimage" &>>/var/log/image_optimizer.log | |
done; | |
else | |
countthiswidth="$imagewidth" | |
countedwidth="$(( ${#countthiswidth} - 1 ))" | |
countthisheight="$imageheight" | |
countedheight="$(( ${#countthisheight} - 1 ))" | |
echo "Lesser than 2000" | |
# The image width minus the screensize? | |
width="$(($imagewidth / $countedwidth))x" | |
# Same as the width | |
height="$(($(exiftool "$imagefile" | grep "Image Height" | awk '{print $4}') / $countedheight))" | |
smallimage="$(echo "$image" | sed "s/\(.*\)\./\1-optimizer-$(echo $width | sed 's/x//g')./g" | awk '{print $1}')" | |
convert "$imagefile" -resize $width$height "$path/$smallimage" &>>/var/log/image_optimizer.log | |
fi | |
;; | |
esac | |
done; | |
echo "Whoewheee... We're done. Now, lets optimize all the *-optimizer-* images!" | |
find $imagedir/ -type f -name "*-optimizer-*.jpg" -exec jpegoptim -m85 -o -p --strip-all --totals "{}" \; &>>/var/log/image_optimizer.log | |
find $imagedir/ -type f -name "*-optimizer-*.png" -exec optipng -o7 -preserve -strip all "{}" \; &>>/var/log/image_optimizer.log | |
end=`date +%s` | |
runtime=$((end-start)) | |
# $runtime only display time spent in seconds, So we will calculate the seconds | |
# to a more 'human readable' format. | |
SECONDS=$runtime | |
duration=$SECONDS | |
echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment