forked from denilsonsa/small_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zopflipng_in_place
executable file
·139 lines (117 loc) · 2.91 KB
/
zopflipng_in_place
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
133
134
135
136
137
138
139
#!/bin/bash
MYNAME=`basename "$0"`
print_help() {
cat << EOF
Usage: $MYNAME [-P <n>] [-k] [zopflipng options] [--] <filename(s)>
This script optimizes one or more PNG images in-place, using zopflipng.
It works by calling zopflipng on each input file, saving the output to a
temporary file, and then writing the temporary file on top of the original file.
-P <n> Run in parallel, with <n> being the number of parallel jobs.
-k Shortcut for --keepchunks=pHYs
-kk Shortcut for --keepchunks=pHYs,tIME
-kkk Shortcut for --keepchunks=pHYs,tIME,bKGD
-- Marks the end of options and the start of filenames.
EOF
}
parse_arguments() {
PARALLEL=0
ZARGS=( )
ZFILES=( )
# Options or filenames, until the first filename, or until --.
while [[ $# != 0 ]] ; do
case "$1" in
-h | -help | --help )
print_help
exit
;;
-prefix* | --prefix*)
echo "${MYNAME}: The --prefix option cannot be used."
exit 1
;;
-y)
echo "${MYNAME}: The -y option cannot be used."
exit 1
;;
-P)
shift
PARALLEL="$1"
;;
-k)
ZARGS+=("--keepchunks=pHYs")
;;
-kk)
ZARGS+=("--keepchunks=pHYs,tIME")
;;
-kkk)
ZARGS+=("--keepchunks=pHYs,tIME,bKGD")
;;
-- )
shift
break
;;
-* )
ZARGS+=("$1")
;;
* )
ZFILES+=("$1")
shift
break
;;
esac
shift
done
# Filenames.
while [[ $# != 0 ]] ; do
ZFILES+=("$1")
shift
done
# Sanity checks:
if [[ "${#ZFILES[@]}" = 0 ]] ; then
echo "${MYNAME}: Missing parameters, use --help for instructions."
exit 1
fi
if ! [[ "${PARALLEL}" =~ ^[0-9][0-9]*$ ]] ; then
echo "${MYNAME}: Invalid -P value: ${PARALLEL}"
exit 1
fi
FILE_ERROR=0
for f in "${ZFILES[@]}" ; do
if [ ! -f "${f}" ] ; then
echo "${MYNAME}: File not found or not a regular file: ${f}"
FILE_ERROR=1
elif [ ! -r "${f}" ] ; then
echo "${MYNAME}: File not readable: ${f}"
FILE_ERROR=1
fi
done
if [ "${FILE_ERROR}" = 1 ] ; then
exit 1
fi
}
# Main code.
parse_arguments "$@"
if ! which zopflipng > /dev/null ; then
echo "${MYNAME}: zopflipng executable not found. You can compile and install it from:"
echo 'https://github.com/google/zopfli/tree/master/src/zopflipng/'
exit 1
fi
if [ "${PARALLEL}" = 0 ] ; then
for f in "${ZFILES[@]}" ; do
(
TMPFILE="$(mktemp)"
trap 'rm -f "${TMPFILE}"' EXIT
# zopflipng does not exit with a non-zero status if an unknown flag is passed.
# For this reason, we check if the temporary file is non-empty.
zopflipng -y "${ZARGS[@]}" "${f}" "${TMPFILE}" && [ -s "${TMPFILE}" ] && {
# Using cat instead of mv in order to preserve permissions of the original file.
cat "${TMPFILE}" > "${f}"
}
)
done
else
# For parallel execution, this script calls itself through xargs.
# https://stackoverflow.com/a/21998589/
for f in "${ZFILES[@]}" ; do
echo "${f}"
done | xargs -d '\n' -n 1 -P "${PARALLEL}" "${0}" "${ZARGS[@]}"
fi