/
image_optim.sh
executable file
·132 lines (111 loc) · 3.01 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
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
#!/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"
}
git_commit()
{
status=`git status --porcelain`
if [ ! -z "${status}" ] ; then
date=`date`
git status
git commit -a -m "image_optim $date"
else # no changes
timeendglobal
echo "Script terminated successfully."
exit
fi
}
jpeg_remove_comment_and_exiv()
{
echo "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
echo "$TD"
}
if [ "`git log -1 --pretty="%B" | awk '{print $1}'| sed '$d'`" == "image_optim" ] ; then
filelist=`git log -1 --stat --pretty="%b" | sed '$d' | awk '{print $1}'`
else
filelist=`git ls-files ./ | grep "\.png$"`
fi
png_optimize_all()
{
if [ ! -z "${filelist}" ] ; then
echo "starting to optimize pngs"
LOGFILE="/tmp/image_optim_png.log"
while [ "${filelist}" != "" ] ; do
numberoffiles=$(echo ${filelist} | wc -w)
echo "starting to optimize ${numberoffiles} pngs."
timestart
echo ${filelist} | xargs -P ${cpucores} -n 1 optipng -zc1-9 -zm1-9 -zs0-3 -f0-5 |& grep "\ Processing" >> "${LOGFILE}"
echo ${filelist} | xargs -P ${cpucores} -n 1 advpng -z4 >> "${LOGFILE}"
# we need to call xargs 2 times: the first time to separate the input strings,
# the second time to have -I {} working to place the inputs name multiple time in the
# output command
echo ${filelist} | xargs -P 1 -n 1 | xargs -P ${cpucores} -n 1 -I '{}' pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time {} {}.foo |& grep -v "\ |\ " >> "${LOGFILE}"
# 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}
timeend
echo "a run optimizing pngs took $TD"
done
git_commit
filelist=`git log -1 --stat --pretty="%b" | sed '$d' | awk '{print $1}'`
png_optimize_all
fi
}
timestartglobal
make_sure_we_are_safe
jpeg_remove_comment_and_exiv
png_optimize_all
git_commit
timeendglobal