-
Notifications
You must be signed in to change notification settings - Fork 0
/
png-optim.sh
executable file
·71 lines (58 loc) · 1.87 KB
/
png-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
#!/bin/sh
if [ -z "$1" ]; then
DIR="`pwd`"
else
DIR="$1"
fi
function each_file() {
local file="$1"
local file_="$1.optimized"
((NUM_FILES++))
sizeA=$( du "$file" | awk '{print $1}' )
# -brute : try 100+ additional optimization methods, slow
# -rem : remove data:
# gAMA : gamma
# cHRM : chroma
# iCCP : ICCP color profile
# sRGB : additional sRGB profile
# alla : all chunks, except transparency
# allb : all chunks, except transparency and gamma
# text : textual chunks like attribution
# -reduce : eliminate unused colors and reduce bit-depth if possible
pngcrush -brute -rem alla -reduce "$file" "$file_"
if [ $? -gt 0 ]; then
echo "$file: pngcrush failed"
((NUM_FAILED++))
rm -f "$file_"
return 1
fi
sizeB=$( du "$file_" | awk '{print $1}' )
if [ $sizeB -lt $sizeA ]; then
local bytesSaved=$(( sizeA - sizeB ))
echo "$file: Saved ${bytesSaved}b"
NUM_BYTES=$(( NUM_BYTES + bytesSaved ))
#chown `ls -ld "$file" | awk '{print $3 ":" $4}'` "$file_"
#chown $(stat -c "%U:%G" "$file.optimized") "$file"
#chmod $(stat -c "%a" "$file.optimized") "$file"
mv -f "$file_" "$file";
elif [ $sizeB -eq $sizeA ]; then
echo "$file: skipping, optimized was same size"
((NUM_SKIPPED++))
else
echo "$file: skipping, optimized was "$(( $sizeB - $sizeA ))"b larger"
((NUM_LARGER++))
fi
rm -f "$file_"
}
find "$DIR" -name '*.png' -type f | {
# Use sub-shell to encapsulate variables
NUM_FILES=0
NUM_SKIPPED=0
NUM_LARGER=0
NUM_FAILED=0
NUM_BYTES=0
while read file; do
each_file "$file"
done
echo "Found $NUM_FILES, skipped $NUM_SKIPPED, enlarged/skipped $NUM_LARGER, failed $NUM_FAILED, saved $NUM_BYTES bytes"
}