-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtinypng
executable file
·112 lines (100 loc) · 2.61 KB
/
tinypng
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
##
# Author: Alex Kulikov <alex.kulikov@xing.com>
# Version: 0.1
# Description: Automated mass optimization script for web images.
#
# Credits:
# - pngcrush by Glenn Randers-Pehrson (http://pmt.sourceforge.net/pngcrush)
# - OptiPNG by Cosmin Truta (http://optipng.sourceforge.net)
# - pngout by Ken Silverman (http://advsys.net/ken/utils.htm)
# - advpng by Andrea Mazzoleni (http://advancemame.sourceforge.net/comp-readme.html)
##
# Global Vars
TARGET=${1%/}
TOTALFILES=0
# Colors
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
COL_CYAN=$ESC_SEQ"36;01m"
#Make Bakup
function backup(){
date=`date "+%y%m%d_%H%M%S"`
PWD=`pwd`
path=$PWD/$TARGET
tar czf /tmp/tinypng_$date.tar.gz $path > /dev/null 2>&1
echo "Backup: /tmp/"$date".tar.gz"
}
#Count PNGs in targeted folder
function count() {
for x in $(find $TARGET -type f -name '*.png'); do
TOTALFILES=$(($TOTALFILES+1))
done
}
#find all PNGs in folder and crush there
function crushdir() {
#Counter Vars
totalstart=0
totalend=0
position=0
#Go!
for x in $(find $TARGET -type f -name '*.png'); do
position=$(($position+1))
percentdone=$(($position*100/$TOTALFILES))
echo "["$position/$TOTALFILES"] ("$percentdone "%)"
count=0
let start=`ls -l $x | tr -s " " | cut -d " " -f 5`
totalstart=$(($totalstart+$start))
#DO IT!
crunshPNG $x
let end=`ls -l pngout.png | tr -s " " | cut -d " " -f 5`
totalend=$(($totalend+$end))
rm $x
mv pngout.png $x
sum=$(($start-$end))
percent=$(($sum*100/$start))
echo $x "Saved:" $sum "Bytes ("$percent "%)"
done
totalsum=$(($totalstart-$totalend))
totalpercent=$(($totalsum*100/$totalstart))
echo -e $COL_CYAN"Total Saved:" $(($totalsum/1024)) "KByte ("$totalsum "Byte) ("$totalpercent"%)"$COL_RESET
}
function crushfile() {
let start=`ls -l $TARGET | tr -s " " | cut -d " " -f 5`
crunshPNG $TARGET
let end=`ls -l pngout.png | tr -s " " | cut -d " " -f 5`
rm $TARGET
mv pngout.png $TARGET
sum=$(($start-$end))
percent=$(($sum*100/$start))
echo $TARGET "Saved:" $sum "Bytes ("$percent "%)"
}
function crunshPNG() {
pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB -brute -l 9 -max -reduce -m 0 -q $1
optipng -o7 -q pngout.png
pngout pngout.png -q -y -k0 -s0
advpng -z -4 pngout.png > /dev/null
}
# Is target a file or a directory
function conditions() {
if [ -d "$TARGET" ]
then
backup
count
crushdir
fi
if [ -f "$TARGET" ]
then
backup
crushfile
fi
}
if [ -w "$TARGET" ]
then
conditions
else
echo -e $COL_RED"No write rights or wrong targed!"$COL_RESET
fi
exit 0