-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimize_images
executable file
·169 lines (139 loc) · 3.44 KB
/
optimize_images
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env sh
optimize_product_images() {
directory=$1
optimized_dir="$directory/optimized"
mkdir -p "$optimized_dir"
for image in "$directory"/*.[jJ][pP][gG]; do
image=$(basename "$image")
new_image=$(echo "$image" | tr '[:upper:]' '[:lower:]')
convert "$directory/$image" -resize 1270x1270 "$optimized_dir/_$new_image" || exit 1
jpeg-recompress \
-m ms-ssim \
-a \
"$optimized_dir/_$new_image" \
"$optimized_dir/$new_image" ||
exit 1
done
rm "$optimized_dir"/_*.jpg
echo "optimized images are in: $optimized_dir" >&2
}
social_media_preview() {
directory=$1
optimized_dir="$directory/optimized"
mkdir -p "$optimized_dir"
for image in "$directory"/*.[jJ][pP][gG]; do
image=$(basename "$image")
new_image=$(echo "$image" | tr '[:upper:]' '[:lower:]')
convert "$directory/$image" \
-resize 1200x630^ \
-gravity north \
-extent 1200x630 \
"$optimized_dir/_$new_image" || exit 1
jpeg-recompress \
-q high \
-a \
"$optimized_dir/_$new_image" \
"$optimized_dir/$new_image" ||
exit 1
done
rm "$optimized_dir"/_*.jpg
echo "optimized images are in: $optimized_dir" >&2
}
optimize_lookbook_images() {
directory=$1
optimized_dir="$directory/optimized"
mkdir -p "$optimized_dir"
for image in "$directory"/*.[jJ][pP][gG]; do
image=$(basename "$image")
new_image=$(echo "$image" | tr '[:upper:]' '[:lower:]')
convert "$directory/$image" -resize 1280x1280 "$optimized_dir/_$new_image" || exit 1
jpeg-recompress \
-q high \
-a \
"$optimized_dir/_$new_image" \
"$optimized_dir/$new_image" ||
exit 1
done
rm "$optimized_dir"/_*.jpg
echo "optimized images are in: $optimized_dir" >&2
}
optimize_slider_images() {
directory=$1
optimized_dir="$directory/optimized"
mkdir -p "$optimized_dir"
for image in "$directory"/*.[jJ][pP][gG]; do
image=$(basename "$image")
echo "optimizing $image file" >&2
new_image=$(echo "$image" | tr '[:upper:]' '[:lower:]')
for color in '#b6664d' '#49594e' '#efdfd7'; do
color_code=$(echo "$color" | cut -c2-)
convert "$directory/$image" \
-resize 1400x852 \
-size 1400x852 "xc:$color" +swap \
-gravity west \
-composite \
-fill "$color" \
-draw "fill-opacity 0.35 rectangle 0,0 1060,852" \
"$optimized_dir/$color_code-$new_image"
done
done
echo "optimized images are in: $optimized_dir" >&2
}
show_help() {
cat <<EOF
Usage: $(basename "$0") <options>
-h, --help Display help
-s, --slider Images optimization for slider
-l, --lookbook Optimize for lookbook and other content where high quality
is needed
-p, --product For product images - they should be square images
Example:
./$(basename "$0") -p
./$(basename "$0") -l some/deeper/directory
EOF
}
parse_command_line() {
ARGS=$(getopt \
-o hslpm \
--long help,slider,lookbook,product,social-media -- "$@")
getopt_exit="$?"
if [ "$getopt_exit" -ne 0 ]; then
show_help
exit 1
fi
eval set -- "$ARGS"
for o; do
case "$o" in
-h | --help)
show_help
exit
;;
-s | --slider)
command="optimize_slider_images"
shift
;;
-l | --lookbook)
command="optimize_lookbook_images"
shift
;;
-m | --social-media)
command="social_media_preview"
shift
;;
-p | --product)
command="optimize_product_images"
shift
;;
--)
shift
break
;;
esac
done
echo "$(realpath "${*:-$PWD}")"
eval "$command $(realpath "${*:-$PWD}")"
}
main() {
parse_command_line "$@"
}
main "$@"