-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpeg-optim.sh
executable file
·87 lines (71 loc) · 2.17 KB
/
jpeg-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
#!/bin/sh
EXTENSIONS="jpe?g"
PROGESSIVE_SIZE_TRIGGER=500000
if [ -z "$1" ]; then
DIR="`pwd`"
else
DIR="$1"
fi
# Called with $1 = name of a file to process
function each_file() {
local file="$1"
local file_="$1.optimized"
((NUM_FILES++))
sizeA=$( du "$file" | awk '{print $1}' )
if [ $sizeA -gt $PROGESSIVE_SIZE_TRIGGER ]; then
xtra="-progressive"
else
xtra=""
fi
jpegtran -optimize $xtra -outfile "$file_" "$file"
if [ $? -gt 0 ]; then
echo "$file: jpegtran 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_"
}
# The find command is different between platforms
if [ "$(uname)" == "Darwin" ]; then
# Is Mac OS X
CMD="find -E '$DIR'"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
# Is Linux
CMD="find '$DIR' -regextype posix-egrep"
fi
# Perform find
eval "$CMD -regex '.*\.($EXTENSIONS)\$' -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"
}
# Rename xxx.jpg.optimized -> xxx.jpg
#find "$DIR" -name '*.optimized' -print0 | while read -d $'\0' file; do
# chown $(stat -c "%U:%G" "${file%.optimized}") "$file"
# chmod $(stat -c "%a" "${file%.optimized}") "$file"
# mv -f "$file" "${file%.optimized}";
#done