-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_webp.sh
More file actions
executable file
·58 lines (51 loc) · 2.22 KB
/
Copy pathgenerate_webp.sh
File metadata and controls
executable file
·58 lines (51 loc) · 2.22 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
#!/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
#==============================================================================
# Exit on errors
set -e
TARGET_PATH="$(cd "$(dirname "$0")" && pwd)/static/*/*"
for file in $TARGET_PATH; do
FILE_PATH=$(readlink -f "$file" | sed 's/\.[^.]*$//')
echo $FILE_PATH
# Strip EXIF data from still images first, read image size
if [[ $file == *.jpg ]] || [[ $file == *.jpeg ]] || [[ $file == *.png ]] || [[ $file == *.tiff ]]; then
IMAGE_SIZE=$(identify -format '%w' "$file")
echo "Stripping EXIF from ${file}..."
exiv2 rm "$file"
fi
# Skip webps, fonts, etc
if [[ $file == *.webp ]] || [[ $file == *.woff ]] || [[ $file == *.woff2 ]]; then
continue
# Check to see if a WEBP of the same name already exists
elif [[ -f "$FILE_PATH.webp" ]]; then
echo "WEBP for $(basename "$file") already exists. Skipping..."
continue
# Convert GIFs
elif [[ $file == *.gif ]]; then
echo "Converting ${file} --> webp"
gif2webp -q 85 "${file}" -o "${FILE_PATH}.webp" -mt -quiet -mixed
# 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 ${file} --> webp"
echo "++++++"
echo "${file}"
echo "${RESIZE}"
echo "${FILE_PATH}.webp"
echo "++++++"
COMMAND="cwebp -q 100 -mt -quiet "${RESIZE}" "${file}" -o "${FILE_PATH}.webp""
eval "${COMMAND}"
echo "Cleaning up original file: ${file}"
rm "${file}"
fi
done