-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathlossless-compress
executable file
·60 lines (47 loc) · 1.27 KB
/
lossless-compress
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
#!/bin/bash
readonly program="$(basename "${0}")"
readonly formats=('jpg' 'png' 'gif')
function depends_on {
local -r all_deps=("${@}")
local missing_deps=()
for dep in "${all_deps[@]}"; do
hash "${dep}" 2> /dev/null || missing_deps+=("${dep}")
done
if [[ "${#missing_deps[@]}" -gt 0 ]]; then
echo 'Missing required tools:' >&2
printf ' %s\n' "${missing_deps[@]}" >&2
exit 1
fi
}
function usage {
echo "
Losslessly compress files. Supported formats:
${formats[*]}
Usage:
${program} <file...>
Options:
-h, --help Show this help.
" | sed -E 's/^ {4}//'
}
if [[ "${#}" -lt 1 ]]; then
usage
exit 1
fi
if [[ "${1}" =~ ^(-h|--help)$ ]]; then
usage
exit 0
fi
depends_on 'jpegtran' 'optipng' 'gifsicle'
for image_file in "${@}"; do
echo "Compressing ${image_file}…"
file_type="$(file --mime-type --brief "${image_file}" | cut -d'/' -f2)"
if [[ "${file_type}" == 'jpeg' ]]; then
jpegtran -copy none -optimize -progressive -outfile "${image_file}" "${image_file}"
elif [[ "${file_type}" == 'png' ]]; then
oxipng --quiet "${image_file}"
elif [[ "${file_type}" == 'gif' ]]; then
gifsicle --optimize=3 --batch "${image_file}"
else
echo "'${file_type}' is not a supported image type."
fi
done