-
Notifications
You must be signed in to change notification settings - Fork 95
/
optimize-image
executable file
·44 lines (38 loc) · 962 Bytes
/
optimize-image
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
#!/bin/bash
#
# Convert JPEG/PNG images to WebP format.
# > Note: Requires `webp`.
#
# - Optimize a specific image:
#
# `optimize-image {{file.jpg}}`
#
# ---
# Author: Nick Plekhanov, https://nikkhan.com/
# License: MIT
# https://github.com/nicksp/dotfiles
set -euo pipefail
RED="$(tput setaf 1)"
UNDERLINE="$(tput sgr 0 1)"
RESET="$(tput sgr0)"
error() {
echo -e "$UNDERLINE$RED$1$RESET\n"
}
# Check if arguments are provided
if [ "$#" -eq 0 ] || [ "$1" = "-h" ]; then
echo "Usage: $(basename $0) <input_file>"
echo
echo "Convert all given images to webp using cwebp."
echo "The output will be stored in the same directory of input file(s)."
echo
echo "Run \"help $(basename $0)\" for more information on a command."
exit 1
fi
if ! command -v cwebp &> /dev/null; then
error "Webp is not installed: brew install webp"
exit 1
fi
for file in "$@"; do
filename=${file%\.*}
cwebp -q 75 -quiet $file -o $(basename ${filename}).webp
done