Repository navigation
Expand file tree
/
Copy pathcompress
More file actions
executable file
·209 lines (173 loc) · 5.65 KB
/
Copy pathcompress
File metadata and controls
executable file
·209 lines (173 loc) · 5.65 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash
# Image optimization script using ImageMagick
# Converts to WebP, caps at 1280px, targets output under 200KB
# Usage: compress <file|folder>
INPUT="${1:-.}"
MAX_WIDTH=1280
MAX_HEIGHT=1280
MAX_SIZE=204800 # 200KB
QUALITY=80
MIN_QUALITY=25
# Check for ImageMagick command (v7 uses 'magick', older uses 'convert')
if command -v magick &> /dev/null; then
IMG_CMD="magick"
ID_CMD="magick identify"
elif command -v convert &> /dev/null; then
IMG_CMD="convert"
ID_CMD="identify"
else
echo "Error: ImageMagick not found. Install with: brew install imagemagick"
exit 1
fi
if [ ! -e "$INPUT" ]; then
echo "Error: '$INPUT' does not exist"
echo "Usage: compress <file|folder>"
exit 1
fi
get_size() {
stat -f%z "$1" 2>/dev/null || stat -c%s "$1" 2>/dev/null
}
convert_at() {
local input="$1"
local resize_w="$2"
local resize_h="$3"
local quality="$4"
local output="$5"
if [ "$resize_w" != "0" ]; then
$IMG_CMD "$input" -resize "${resize_w}x${resize_h}>" -quality "$quality" "$output"
else
$IMG_CMD "$input" -quality "$quality" "$output"
fi
}
optimize_image() {
local input="$1"
local output="${input%.*}.webp"
local temp_output="${input%.*}_temp.webp"
local orig_size
orig_size=$(get_size "$input")
local orig_kb=$((orig_size / 1024))
echo -n " $(basename "$input") (${orig_kb}KB) → "
# Get original dimensions
local width
local height
width=$($ID_CMD -format "%w" "$input" 2>/dev/null)
height=$($ID_CMD -format "%h" "$input" 2>/dev/null)
if [ -z "$width" ] || [ -z "$height" ]; then
echo "SKIPPED (could not read dimensions)"
return 1
fi
# Determine working dimensions: cap to MAX but no upscale
local work_w=$MAX_WIDTH
local work_h=$MAX_HEIGHT
if [ "$width" -le "$work_w" ] && [ "$height" -le "$work_h" ]; then
# Already small enough, no resize needed
work_w=0
work_h=0
fi
# --- Pass 1: Try initial quality with resize ---
convert_at "$input" "$work_w" "$work_h" "$QUALITY" "$temp_output"
local size
size=$(get_size "$temp_output")
if [ "$size" -le "$MAX_SIZE" ]; then
mv "$temp_output" "$output"
local final_kb=$((size / 1024))
echo "${final_kb}KB (q${QUALITY})"
return 0
fi
# --- Pass 2: Binary search quality from QUALITY down to MIN_QUALITY ---
local low=$MIN_QUALITY
local high=$QUALITY
local best_q=0
while [ "$low" -le "$high" ]; do
local mid=$(((low + high) / 2))
convert_at "$input" "$work_w" "$work_h" "$mid" "$temp_output"
size=$(get_size "$temp_output")
if [ "$size" -le "$MAX_SIZE" ]; then
best_q=$mid
low=$((mid + 1))
else
high=$((mid - 1))
fi
done
if [ "$best_q" -gt 0 ]; then
convert_at "$input" "$work_w" "$work_h" "$best_q" "$temp_output"
mv "$temp_output" "$output"
local final_kb=$(($(get_size "$output") / 1024))
echo "${final_kb}KB (q${best_q})"
return 0
fi
# --- Pass 3: Progressive downsize ---
# Quality alone couldn't do it. Shrink dimensions in steps and retry.
local scale_steps="0.75 0.6 0.5 0.4 0.33"
if [ "$work_w" = "0" ]; then
# Image was already within max dims, so start from actual size
work_w=$width
work_h=$height
fi
for scale in $scale_steps; do
local scale_pct
local new_w
local new_h
scale_pct=${scale#0.}
new_w=$((work_w * scale_pct / 100))
new_h=$((work_h * scale_pct / 100))
# Don't go below 320px wide
if [ "$new_w" -lt 320 ]; then
break
fi
# Try at initial quality first
convert_at "$input" "$new_w" "$new_h" "$QUALITY" "$temp_output"
size=$(get_size "$temp_output")
if [ "$size" -le "$MAX_SIZE" ]; then
mv "$temp_output" "$output"
local final_kb=$((size / 1024))
echo "${final_kb}KB (${new_w}x${new_h}, q${QUALITY})"
return 0
fi
# Binary search quality at this smaller size
low=$MIN_QUALITY
high=$QUALITY
best_q=0
while [ "$low" -le "$high" ]; do
local mid=$(((low + high) / 2))
convert_at "$input" "$new_w" "$new_h" "$mid" "$temp_output"
size=$(get_size "$temp_output")
if [ "$size" -le "$MAX_SIZE" ]; then
best_q=$mid
low=$((mid + 1))
else
high=$((mid - 1))
fi
done
if [ "$best_q" -gt 0 ]; then
convert_at "$input" "$new_w" "$new_h" "$best_q" "$temp_output"
mv "$temp_output" "$output"
local final_kb=$(($(get_size "$output") / 1024))
echo "${final_kb}KB (${new_w}x${new_h}, q${best_q})"
return 0
fi
done
# If we get here, something went very wrong
rm -f "$temp_output"
echo "FAILED (could not get under 200KB)"
return 1
}
# --- Main ---
if [ -f "$INPUT" ]; then
optimize_image "$INPUT"
else
extensions="jpg jpeg png gif bmp tiff webp"
echo "compress -> $INPUT (max ${MAX_WIDTH}px, <200KB)"
echo "---------------------------------------------------"
local_count=0
for ext in $extensions; do
ext_upper=$(echo "$ext" | tr '[:lower:]' '[:upper:]')
for img in "$INPUT"/*."$ext" "$INPUT"/*."$ext_upper"; do
[ -f "$img" ] || continue
local_count=$((local_count + 1))
optimize_image "$img"
done
done
echo "---------------------------------------------------"
echo "Done. ${local_count} images processed."
fi