-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_optim.sh
executable file
·106 lines (89 loc) · 2.27 KB
/
image_optim.sh
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
#!/bin/bash
#TODO: also use pngout ?
echo "To kill the script, run"
echo "while true ; do killall optipng ; killall advpng ; killall pngcrush ; done"
echo "for a while"
VERBOSE=true
cpucores=`getconf _NPROCESSORS_ONLN`
timestartglobal()
{
TSG=`date +%s.%N`
}
timeendglobal()
{
TEG=`date +%s.%N`
TDG=`calc $TEG - $TSG`
echo "$TDG"
}
timestart()
{
TS=`date +%s.%N`
}
timeend()
{
TE=`date +%s.%N`
TD=`calc $TE - $TS`
}
print()
{
${VERBOSE} && echo $1
}
make_sure_we_are_safe()
{
if [[ $(git rev-parse --is-inside-work-tree) != "true" ]] >& /dev/null ; then
echo "fatal: Not a git repository!"
echo "Make sure to be in a git repo!"
echo "Exiting"
exit 2
fi
echo "WARNING, this script is supposed to be run in a git repo"
echo "We will create a new branch now which the scriptwill work in."
git branch script/image_optim
git checkout script/image_optim
echo "Done"
}
jpeg_remove_comment_and_exiv()
{
print "Removing comments and exiv data from jpegs."
timestart
git ls-files ./ | grep -e "\.jpg$" -e "\.jpeg" | xargs -P ${cpucores} -n 1 jpegoptim --strip-all >> /tmp/image_optim_jpeg.log
timeend
wait
print "$TD"
}
png_optimize_all()
{
print "starting to optimize pngs"
filelist=$(git ls-files ./ | grep "\.png$")
while [ "${filelist}" != "" ] ; do
numberoffiles=$(echo ${filelist} | wc -w)
print "starting to optimize ${numberoffiles} pngs."
timestart
cat ${filelist} | xargs -P ${cpucores} -n 1 optipng -zc1-9 -zm1-9 -zs0-3 -f0-5 >> /tmp/image_optim_png.log
cat ${filelist} | xargs -P ${cpucores} -n 1 advpng -z4 >> /tmp/image_optim_png.log
cat ${filelist} | xargs -P ${cpucores} -n 1 -I '{}' pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time {} {}.foo >> /tmp/image_optim_png.log
wait
# deciding for which file to use is easy for cpu,
# waiting for i/o, no need to parallelize it.
newfilelist=""
for i in ${filelist} ; do
if [[ `du -b $i | awk '{print $1}'` -gt `du -b $i.foo | awk '{print $1}'` ]] ; then
mv $i.foo $i
newfilelist="${newfilelist} $i"
else
rm $i.foo
fi
done
filelist=${newfilelist}
date=`date`
git commit -a -m "image_optim $date"
timeend
print "a run optimizing pngs took $TD"
done
wait
}
timestartglobal
make_sure_we_are_safe
jpeg_remove_comment_and_exiv
png_optimize_all
timeendglobal