forked from vitorgalvao/tiny-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlossless-compress
More file actions
executable file
·64 lines (51 loc) · 1.49 KB
/
Copy pathlossless-compress
File metadata and controls
executable file
·64 lines (51 loc) · 1.49 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
readonly program="$(basename "${0}")"
readonly formats=('jpg' 'png' 'gif' 'svg')
function depends_on {
readonly local all_deps=("${@}")
local missing_deps=()
for dep in "${all_deps[@]}"; do
command -v "${dep}" &>/dev/null || missing_deps+=("${dep}")
done
if [[ "${#missing_deps[@]}" -gt 0 ]]; then
tput setaf 1
echo -e '\nThis script has unmet dependencies. You need to install these first:'
printf ' %s\n' "${missing_deps[@]}"
tput sgr0
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' ]] || [[ "${1}" == '--help' ]]; then
usage
exit 0
fi
depends_on 'jpegtran' 'optipng' 'gifsicle' 'svgcleaner'
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
optipng -quiet -o7 "${image_file}"
elif [[ "${file_type}" == 'gif' ]]; then
gifsicle --optimize=3 --batch "${image_file}"
elif [[ "${file_type}" == 'svg+xml' ]]; then
svgcleaner --quiet "${image_file}" "${image_file}"
else
echo "'${file_type}' is not a supported image type."
fi
done