size reduction images

This commit is contained in:
Simon Martens
2025-09-29 23:30:34 +02:00
parent 269d635f1d
commit 9a856527cc
5 changed files with 96 additions and 66 deletions

View File

@@ -1,6 +1,7 @@
#!/bin/bash
# Script to generate WebP preview images from existing JPEG files
# Resizes images to 50% and applies high compression for fast layout loading
# Usage: ./scripts/generate_webp_previews.sh
# Colors for output
@@ -23,6 +24,15 @@ if ! command -v cwebp &> /dev/null; then
exit 1
fi
# Check if ImageMagick identify is installed (for resizing)
if ! command -v identify &> /dev/null; then
echo -e "${YELLOW}Warning: ImageMagick identify not found. Resizing will be skipped.${NC}"
echo " Ubuntu/Debian: sudo apt-get install imagemagick"
echo " macOS: brew install imagemagick"
echo " CentOS/RHEL: sudo yum install ImageMagick"
echo ""
fi
# Check if pictures directory exists
if [ ! -d "$PICTURES_DIR" ]; then
echo -e "${RED}Error: Pictures directory '$PICTURES_DIR' not found${NC}"
@@ -32,6 +42,7 @@ fi
echo -e "${GREEN}Generating WebP preview images...${NC}"
echo "Quality: $QUALITY%"
echo "Compression: $COMPRESSION"
echo "Resize: 50% (for faster loading)"
echo ""
# Counters
@@ -54,16 +65,28 @@ process_file() {
name_no_ext="${filename%.*}"
webp_file="$dir/${name_no_ext}-preview.webp"
# Skip if WebP preview already exists and is newer than source
if [ -f "$webp_file" ] && [ "$webp_file" -nt "$jpg_file" ]; then
echo -e "${YELLOW}Skipping $jpg_file (preview exists and is newer)${NC}"
return 0
# Check if WebP preview already exists
if [ -f "$webp_file" ]; then
echo -e "${YELLOW}Overriding existing preview: $webp_file${NC}"
fi
# Get image dimensions and calculate 50%
dimensions=$(identify -ping -format "%w %h" "$jpg_file" 2>/dev/null)
if [ $? -eq 0 ] && [ -n "$dimensions" ]; then
width=$(echo $dimensions | cut -d' ' -f1)
height=$(echo $dimensions | cut -d' ' -f2)
new_width=$((width / 2))
new_height=$((height / 2))
resize_params="-resize $new_width $new_height"
echo "Processing: $jpg_file -> $webp_file (${width}x${height}${new_width}x${new_height})"
else
# Fallback: no resizing if we can't get dimensions
resize_params=""
echo "Processing: $jpg_file -> $webp_file (no resize - couldn't get dimensions)"
fi
# Convert to WebP
echo "Processing: $jpg_file -> $webp_file"
if cwebp -q "$QUALITY" -m "$COMPRESSION" "$jpg_file" -o "$webp_file" 2>/dev/null; then
if cwebp -q "$QUALITY" -m "$COMPRESSION" $resize_params "$jpg_file" -o "$webp_file" 2>/dev/null; then
# Check file sizes
jpg_size=$(stat -f%z "$jpg_file" 2>/dev/null || stat -c%s "$jpg_file" 2>/dev/null)
webp_size=$(stat -f%z "$webp_file" 2>/dev/null || stat -c%s "$webp_file" 2>/dev/null)