-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_webp.sh
49 lines (44 loc) · 2.16 KB
/
generate_webp.sh
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
#!/bin/bash
#==============================================================================
#title :generate_webp.sh
#description :Converts still and gif images to webp. Resizes if they are large
#usage :Run this script with WSL on Windows or Bash shell on Unix/Linux
# :This is intended to be run from the root website directory
# :Adjust target location for conversion as needed
#prerequisite :webp,imagemagick and exiv2 installed
#==============================================================================
TARGET_PATH="$(cd "$(dirname "$0")" && pwd)/assets/img/remote/*"
for file in $TARGET_PATH; do
DUP_CHECK=$(readlink -f "$file" | sed 's/\.[^.]*$//')
IMAGE_SIZE=$(identify -format '%w' "$file")
# Strip EXIF data from still images first
if [[ $file != *.gif ]] || [[ $file != *.webp ]]; then
echo "Stripping EXIF from $(basename "$file")..."
exiv2 rm "$file"
fi
# Skip webps
if [[ $file == *.webp ]]; then
continue
# Check to see if a WEBP of the same name already exists
elif [[ -f "$DUP_CHECK.webp" ]]; then
echo "WEBP for $(basename "$file") already exists. Skipping..."
continue
# Convert GIFs
elif [[ $file == *.gif ]]; then
echo "Converting ./assets/img/remote/$(basename "${file}") --> webp"
gif2webp -q 90 "$(echo "$file" | sed -e 's/\*/ /g' -e 's/^"//' -e 's/"$//')" \
-o "$(echo "$file" | sed -e 's/\*/ /g' -e 's/^"//' | sed 's/.[^.]*$//').webp" \
-mt -quiet
# Convert still images
elif [[ $file == *.jpg ]] || [[ $file == *.jpeg ]] || [[ $file == *.png ]] || [[ $file == *.tiff ]]; then
# Resize large images (Width greater than 800 pixels)
if [ "${IMAGE_SIZE}" -gt 800 ]; then
echo "Image width greater than 800, resizing..."
RESIZE='-resize 800 0'
fi
echo "Converting ./assets/img/remote/$(basename "${file}") --> webp"
cwebp -q 100 "$(echo "$file" | sed -e 's/\*/ /g' -e 's/^"//' -e 's/"$//')" \
-o "$(echo "$file" | sed -e 's/\*/ /g' -e 's/^"//' | sed 's/.[^.]*$//').webp" \
$RESIZE -mt -quiet
fi
done