-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimg
executable file
·121 lines (101 loc) · 2.78 KB
/
optimg
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
#!/usr/bin/env bash
#
# Optimize images by compressing & renaming.
#
# Paul Esch-Laurent <p@uly.io>
# MIT
#
# Requires `imagemin`: npm i -g imagemin-cli
# Strict settings
set -o errexit
set -o pipefail
set -o nounset
# Magic variables
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename "${__file}" .sh)"
# Output directory
OUT_DIR="_optimg"
# Print out usage information
usage() {
cat <<HEREDOC
Usage:
${__base} <files>
<files> can be individual files or globs (e.g. *.jpg, foo/*).
Options:
-h --help Show this.
-r --rename Just rename, don't compress.
HEREDOC
}
# Check for required binary
check_reqs() {
if [[ ! $(type -P "imagemin") ]]; then
echo "'imagemin' required. Install with npm: npm i -g imagemin-cli"
exit 1
fi
}
# Create output directory if necessary
create_directory() {
if [[ ! -d "${OUT_DIR}" ]]; then
mkdir -p "${OUT_DIR}"
echo "Created output directory: ${OUT_DIR}"
fi
}
# Rename 'em up
rename_images() {
create_directory
for file in "${@:-}"; do
# It's probably an image and it's not a directory
if file "${file}" | grep -qE 'image|bitmap' && [[ ! -d "${file}" ]]; then
# First 7 characters of SHA2 hash
hash="$(openssl sha256 "${file}" | tr -d '\n' | tail -c 64 | head -c 7)"
full="$(basename "${file}")"
# Get extension (... and convert jpeg -> jpg)
ext="$(echo "${full##*.}" | sed -e 's/e//g')"
# Remove non- alphanumerics, periods, underscores, and dashes
name="$(echo "${full%.*}" | sed -e 's/[^A-Za-z0-9._-]//g')"
echo "Renaming ${file}"
cp "${file}" "${OUT_DIR}/${name}.${hash}.${ext,,}"
fi
done
}
# Optimize (compress & rename) 'em up
optimize_images() {
create_directory
pids=()
for file in "${@:-}"; do
# It's probably an image and it's not a directory
if file "${file}" | grep -qE 'image|bitmap' && [[ ! -d "${file}" ]]; then
# First 7 characters of SHA2 hash
hash="$(openssl sha256 "${file}" | tr -d '\n' | tail -c 64 | head -c 7)"
full="$(basename "${file}")"
# Get extension (... and convert jpeg -> jpg)
ext="$(echo "${full##*.}" | sed -e 's/e//g')"
# Remove non- alphanumerics, periods, underscores, and dashes
name="$(echo "${full%.*}" | sed -e 's/[^A-Za-z0-9._-]//g')"
echo "Optimizing ${file}"
imagemin "${file}" > "${OUT_DIR}/${name}.${hash}.${ext,,}" &
pids+=("${!}")
fi
done
for pid in "${pids[@]}"; do
wait "${pid}"
done
}
# Drive it
main() {
check_reqs
# Parse args
if [[ -z "${1:-}" ]]; then
usage
elif [[ "${1:-}" =~ ^-h|--help$ ]]; then
usage
elif [[ "${1:-}" =~ ^-r|--rename$ ]]; then
shift
rename_images "${@:-}"
else
optimize_images "${@:-}"
fi
}
# Call it
main "${@:-}"