-
Notifications
You must be signed in to change notification settings - Fork 34
/
image-compact
executable file
·45 lines (37 loc) · 1.3 KB
/
image-compact
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
#!/bin/bash
sz=1920x1440
usage() {
local bin=$(basename $0)
[[ $1 -eq 0 ]] || exec 1>&2
echo "Usage: [SZ=$sz] $bin /path/to/some/image.jpg ..."
echo
echo "SZ env-var can be a size for square to fit image into, or WxH spec"
echo " for rectangle with max width/height. Scaling is always proportional."
echo "Uses imagemagick with jpg destination and quality=80, and jpegoptim on that."
exit ${1:-1}; }
[[ "$1" = -h || "$1" = --help ]] && usage 0
[[ "$#" -ge 1 ]] || usage 1
set -eo pipefail
du_hs() {
local du dup
read -ra du dup < <(du -hs "$1" || kill -INT $$)
printf '%s' "$du"; }
[[ -z "$SZ" ]] || sz=$SZ
[[ "$sz" == *x* ]] || sz="${sz}x${sz}"
for p in "$@"; do
[[ -e "$p" ]] || { echo >&2 "ERROR: nx input file - '$p'"; exit 1; }
[[ ! -e "$p".fix.jpg ]] || {
echo >&2 "ERROR: temp-file from earlier bug - $p.fix.jpg"; exit 1; }
du1=$(du_hs "$p")
magick "$p" -resize "${sz}>" -quality 80 "$p".fix.jpg
jpegoptim -q --strip-all --all-normal --force "$p".fix.jpg
touch -m --reference="$p" "$p".fix.jpg
mv "$p".fix.jpg "$p"
p0=$p ext="${p##*.}"
[[ "$ext" = jpg ]] && ext= || {
if [[ ${#ext} -gt 4 ]]
then echo >&2 "WARN: no proper filename.ext on [ $p ]"; ext=" :: $ext ???"
else p=${p%.*}.jpg; mv "$p0" "$p"; ext=" :: $ext -> jpg"; fi; }
du2=$(du_hs "$p")
echo "$p0 :: $du1 -> $du2$ext"
done