35 lines
1003 B
Bash
35 lines
1003 B
Bash
#!/bin/ash
|
|
# Converts all JPG and PNG images in /public to WebP format.
|
|
|
|
PUBLIC_DIR="./public"
|
|
WEBP_QUALITY=80
|
|
|
|
# Check if cwebp command is available
|
|
if ! command -v cwebp &> /dev/null
|
|
then
|
|
echo "Error: 'cwebp' command not found. Please install the 'webp' package."
|
|
exit 1
|
|
fi
|
|
|
|
echo "Searching for images to convert to WebP in $PUBLIC_DIR..."
|
|
|
|
# Find all jpg, jpeg, and png files, and ignore node_modules just in case
|
|
find "$PUBLIC_DIR" -type f \( -name "*.jpg" -o -name "*.jpeg" -o -name "*.png" \) | while read -r img_file; do
|
|
output_webp="${img_file%.*}.webp"
|
|
|
|
# Only create the image if it doesn't already exist
|
|
if [ ! -f "$output_webp" ]; then
|
|
echo "Converting: $img_file"
|
|
cwebp -q "$WEBP_QUALITY" "$img_file" -o "$output_webp"
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo " -> Created: $output_webp"
|
|
else
|
|
echo " -> Error: Failed to create WebP for $img_file."
|
|
fi
|
|
else
|
|
echo "Skipping: $output_webp already exists."
|
|
fi
|
|
done
|
|
|
|
echo "WebP conversion finished." |