-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptimg.bash
More file actions
executable file
·45 lines (37 loc) · 1.15 KB
/
optimg.bash
File metadata and controls
executable file
·45 lines (37 loc) · 1.15 KB
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
#!/bin/bash
#
# Optimize the images
#
function optimg()
{
local input_file_size=
local output_file_size=
local output
local percent
local diff=0
local width=
local pad=
width=$( tput cols )
pad=$( printf '%*s' "$width" | tr " " "." )
for img in "$@"; do
if [ ! -f "$img" ]; then
continue;
fi
input_file_size=$(stat -c%s "$img")
if [ "${img##*.}" = "png" ]; then
output="PNG-image: $img "
optipng -o1 --quiet "$img"
pngcrush -q "$img" /tmp/$$; mv /tmp/$$ "$img"
elif [ "${img##*.}" = "jpg" -o "${img##*.}" = "jpeg" ]; then
output="JPEG-image: $img "
jpegtran -copy none -optimize -outfile /tmp/$$ "$img"; mv /tmp/$$ "$img"
else
continue;
fi
output_file_size=$(stat -c%s "$img")
diff=$(( diff + input_file_size - output_file_size ))
percent=$( echo "scale=2; $output_file_size*100/$input_file_size" | bc )
printf '%s %*.*s %6s\n' "$output" 0 $(( ${#pad} - ${#output} - 8 )) "$pad" "$percent"
done
echo "Saved " $(( diff / 1000 )) " kB."
}