|
|
#!/bin/bash |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set -e |
|
|
|
|
|
IMAGES_DIR="./images" |
|
|
|
|
|
|
|
|
if [ ! -d "$IMAGES_DIR" ]; then |
|
|
echo "Error: $IMAGES_DIR directory not found" |
|
|
exit 1 |
|
|
fi |
|
|
|
|
|
|
|
|
if ! command -v cwebp &> /dev/null && ! command -v convert &> /dev/null; then |
|
|
echo "Error: Neither cwebp nor ImageMagick (convert) found. Please install one of them." |
|
|
exit 1 |
|
|
fi |
|
|
|
|
|
echo "Converting all PNG images to WebP format..." |
|
|
|
|
|
CONVERTED=0 |
|
|
SKIPPED=0 |
|
|
|
|
|
for img in "$IMAGES_DIR"/*.png; do |
|
|
|
|
|
[ -f "$img" ] || continue |
|
|
|
|
|
|
|
|
filename=$(basename "$img" .png) |
|
|
output="${IMAGES_DIR}/${filename}.webp" |
|
|
|
|
|
|
|
|
if [ -f "$output" ]; then |
|
|
echo "Skipping $filename.png (WebP already exists)" |
|
|
SKIPPED=$((SKIPPED + 1)) |
|
|
continue |
|
|
fi |
|
|
|
|
|
echo "Converting: $filename.png -> $filename.webp" |
|
|
|
|
|
|
|
|
if command -v cwebp &> /dev/null; then |
|
|
cwebp -q 85 "$img" -o "$output" 2>/dev/null |
|
|
else |
|
|
convert "$img" -quality 85 "$output" |
|
|
fi |
|
|
|
|
|
|
|
|
if [ -f "$output" ]; then |
|
|
rm "$img" |
|
|
CONVERTED=$((CONVERTED + 1)) |
|
|
else |
|
|
echo "Error: Failed to convert $filename.png" |
|
|
fi |
|
|
done |
|
|
|
|
|
echo "" |
|
|
echo "Conversion complete!" |
|
|
echo "Images converted: $CONVERTED" |
|
|
echo "Images skipped: $SKIPPED" |
|
|
|