forked from tiff/TinyPNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinypng
executable file
·154 lines (137 loc) · 4.23 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
##
# Author: Alex Kulikov <alex.kulikov@xing.com>
# Version: 0.3
# 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)
# - Imagemagick by ImageMagick Studio LLC (http://www.imagemagick.org/script/index.php)
##
# Global Vars
TARGET=${1%/}
TOTALFILES=0
LOGFILE="/tmp/tinypng.log"
ERRORLOG="/tmp/tinypng_error.log"
DATE=`date "+%y%m%d_%H%M%S"`
BACKUP="/tmp/tinypng_"$DATE
PWD=`pwd`
path=$PWD/$TARGET
# 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"
#Copy original files
function copy() {
echo "tinypng: Make backup copy"
mkdir $BACKUP
cp -r $path $BACKUP
}
#Compress orgininal copy
function backup() {
echo "tinypng: Compress backup"
tar czf /tmp/tinypng_$DATE.tar.gz /tmp/tinypng_$DATE 2> /dev/null
rm -r /tmp/tinypng_$DATE/
}
#Count PNGs in targeted folder
function count() {
echo "tinypng: Count files"
for x in $(find $TARGET -type f -name '*.png' ! -name '._*' ); do
TOTALFILES=$(($TOTALFILES+1))
done
}
#find all PNGs in folder and crush there
function crushdir() {
echo "tinypng: Crush directory"
mkdir -p .tinypngtmp/$TARGET
#Counter Vars
totalstart=0
totalend=0
position=0
#Seperator in Logfile
echo "====================" $DATE "====================" >> $LOGFILE
for x in $(find $TARGET -type f -name '*.png' ! -name '._*' ); do
position=$(($position+1))
percentdone=$(($position*100/$TOTALFILES))
echo -ne "["$position/$TOTALFILES"] ("$percentdone "%) >> "$x
count=0
let start=`ls -l $x | tr -s " " | cut -d " " -f 5`
totalstart=$(($totalstart+$start))
crunshPNG $x
let end=`ls -l $x | tr -s " " | cut -d " " -f 5`
totalend=$(($totalend+$end))
sum=$(($start-$end))
percent=$(($sum*100/$start))
wrongsize=$(($sum<0))
if [[ $wrongsize -eq 1 ]] ; then
echo -e $COL_RED"ERROR: "$x" seems to be broken"$COL_RESET
echo "ERROR: "$x" seems to be broken" >> $ERRORLOG
cp $BACKUP/$x $path/
echo ">> "$x" Restored from Backup."
else
echo $x "Saved:" $sum "Bytes ("$percent "%)" >> $LOGFILE
echo " >> Saved:" $sum "Bytes ("$percent "%)"
fi
done
totalsum=$(($totalstart-$totalend))
totalpercent=$(($totalsum*100/$totalstart))
echo "Total Saved:" $(($totalsum/1024)) "KByte ("$totalsum "Byte) ("$totalpercent"%)" >> $LOGFILE
echo -ne '\n'
echo -e $COL_CYAN"Total Saved:" $(($totalsum/1024)) "KByte ("$totalsum "Byte) ("$totalpercent"%)"$COL_RESET
echo "Backup: /tmp/"$DATE".tar.gz" >> $LOGFILE
}
function crushfile() {
echo "tinypng: Crush file"
let start=`ls -l $TARGET | tr -s " " | cut -d " " -f 5`
crunshPNG $TARGET
let end=`ls -l $TARGET | tr -s " " | cut -d " " -f 5`
sum=$(($start-$end))
percent=$(($sum*100/$start))
echo $TARGET "Saved:" $sum "Bytes ("$percent "%)"
echo $TARGET "Saved:" $sum "Bytes ("$percent "%)" >> $LOGFILE
echo "Backup: /tmp/"$DATE".tar.gz" >> $LOGFILE
}
function crunshPNG() {
EXTLESS=`echo $1 | sed -e 's/\.png$//'`
convert $1 -background Black -alpha Background .tinypngtmp/$1
pngquant --ext '-pngquant.png' 256 .tinypngtmp/$1
pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB -brute -l 9 -max 8192 -reduce -m 1 -q .tinypngtmp/$EXTLESS-pngquant.png .tinypngtmp/$EXTLESS-pngcrush.png
optipng -o7 -q -out .tinypngtmp/$EXTLESS-optipng.png .tinypngtmp/$EXTLESS-pngcrush.png
pngout -q -y -k0 -s0 .tinypngtmp/$EXTLESS-optipng.png .tinypngtmp/$EXTLESS-pngout.png
advpng -z -4 .tinypngtmp/$EXTLESS-pngout.png > /dev/null
mv .tinypngtmp/$EXTLESS-pngout.png $1
}
function cleanup() {
rm -rf .tinypngtmp
}
# Is target a file or a directory
function conditions() {
mkdir -p .tinypngtmp
if [ -d "$TARGET" ]
then
copy
count
crushdir
backup
cleanup
fi
if [ -f "$TARGET" ]
then
copy
crushfile
backup
cleanup
fi
}
if [ -w "$TARGET" ]
then
conditions
else
echo -e $COL_RED"No write rights or wrong target!"$COL_RESET
fi
exit 0