~seirdy/dotfiles

ref: 3b722a843f3945a1bdf98672e09786f0213ec6f6 dotfiles/Executables/shell-scripts/bin/optimize-image -rwxr-xr-x 870 bytes
3b722a84Rohan Kumar New script: optimize-image 8 months ago
                                                                                
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
#!/bin/sh
# A helpful comment

# the name of this program
progname="$(basename "${0}")"

help_text="Usage: $progname [OPTION...]

Summary

Options:
  -h, --help            Print this help and exit
"

usage() {
	printf '%s' "$help_text"
}

# when the user passess bad args, send a msg to stderr and exit
# usage: bad_option <option> <reason>
bad_option() {
	echo "$progname: option $1: $2" >&2
	usage >&2
	exit 1
}

# parse arguments
while [ $# -gt 0 ]; do
	case "$1" in
		-h | --help)
			usage
			exit 0
			;;
		*)
			image="$1"
			;;
	esac
	shift
done

mimetype="$(file --dereference --brief --mime-type -- "$image")"
case "$mimetype" in
	image/png)
		oxipng -Z -o max --strip all "$image"
		;;
	image/webp)
		cwebp -z 9 "$image" -o "$image"
		;;
	image/jpeg)
		jpegoptim -s "$image"
		;;
	*)
		echo "unsupported image format: $mimetype"
		exit 1
		;;
esac

# vi:ft=sh