mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-10-29 17:15:31 +00:00
+Compressed IMG files
This commit is contained in:
127
scripts/generate_webp_previews.sh
Executable file
127
scripts/generate_webp_previews.sh
Executable file
@@ -0,0 +1,127 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Script to generate WebP preview images from existing JPEG files
|
||||
# Usage: ./scripts/generate_webp_previews.sh
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Configuration
|
||||
QUALITY=75 # WebP quality (0-100)
|
||||
COMPRESSION=6 # WebP compression level (0-6, higher = better compression)
|
||||
PICTURES_DIR="pictures"
|
||||
|
||||
# Check if cwebp is installed
|
||||
if ! command -v cwebp &> /dev/null; then
|
||||
echo -e "${RED}Error: cwebp is not installed. Please install WebP tools:${NC}"
|
||||
echo " Ubuntu/Debian: sudo apt-get install webp"
|
||||
echo " macOS: brew install webp"
|
||||
echo " CentOS/RHEL: sudo yum install libwebp-tools"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if pictures directory exists
|
||||
if [ ! -d "$PICTURES_DIR" ]; then
|
||||
echo -e "${RED}Error: Pictures directory '$PICTURES_DIR' not found${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -e "${GREEN}Generating WebP preview images...${NC}"
|
||||
echo "Quality: $QUALITY%"
|
||||
echo "Compression: $COMPRESSION"
|
||||
echo ""
|
||||
|
||||
# Counters
|
||||
processed=0
|
||||
skipped=0
|
||||
errors=0
|
||||
|
||||
# Function to process a single file
|
||||
process_file() {
|
||||
local jpg_file="$1"
|
||||
|
||||
# Skip if already a preview file
|
||||
if [[ "$jpg_file" =~ -preview\.(jpg|jpeg)$ ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Generate output filename
|
||||
dir=$(dirname "$jpg_file")
|
||||
filename=$(basename "$jpg_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
|
||||
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
|
||||
# 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)
|
||||
|
||||
if [ -n "$jpg_size" ] && [ -n "$webp_size" ]; then
|
||||
reduction=$(( (jpg_size - webp_size) * 100 / jpg_size ))
|
||||
echo -e "${GREEN} ✓ Success! Size reduction: ${reduction}%${NC}"
|
||||
else
|
||||
echo -e "${GREEN} ✓ Success!${NC}"
|
||||
fi
|
||||
return 0
|
||||
else
|
||||
echo -e "${RED} ✗ Failed to convert $jpg_file${NC}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Export the function and variables for parallel execution
|
||||
export -f process_file
|
||||
export QUALITY COMPRESSION GREEN RED YELLOW NC
|
||||
|
||||
# Detect number of CPU cores for parallel processing
|
||||
CPU_CORES=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
|
||||
PARALLEL_JOBS=$((CPU_CORES))
|
||||
|
||||
echo "Using $PARALLEL_JOBS parallel jobs (detected $CPU_CORES CPU cores)"
|
||||
echo ""
|
||||
|
||||
# Find all JPG files and process them in parallel
|
||||
find "$PICTURES_DIR" -type f \( -name "*.jpg" -o -name "*.jpeg" \) | \
|
||||
grep -v -E '\-preview\.(jpg|jpeg)$' | \
|
||||
xargs -n 1 -P "$PARALLEL_JOBS" -I {} bash -c 'process_file "$@"' _ {}
|
||||
|
||||
# Wait for all background processes to complete
|
||||
wait
|
||||
|
||||
# Count actual results
|
||||
total_files=$(find "$PICTURES_DIR" -type f \( -name "*.jpg" -o -name "*.jpeg" \) | grep -v -E '\-preview\.(jpg|jpeg)$' | wc -l)
|
||||
preview_files=$(find "$PICTURES_DIR" -type f -name "*-preview.webp" | wc -l)
|
||||
processed=$preview_files
|
||||
skipped=0
|
||||
errors=$((total_files - processed))
|
||||
|
||||
# Final summary
|
||||
echo ""
|
||||
echo -e "${GREEN}=== Summary ===${NC}"
|
||||
echo "Processed: $processed files"
|
||||
echo "Skipped: $skipped files"
|
||||
echo "Errors: $errors files"
|
||||
|
||||
if [ $errors -eq 0 ]; then
|
||||
echo -e "${GREEN}All conversions completed successfully!${NC}"
|
||||
else
|
||||
echo -e "${YELLOW}Completed with $errors errors${NC}"
|
||||
fi
|
||||
|
||||
# Calculate total space saved (optional)
|
||||
echo ""
|
||||
echo "To see space savings, run:"
|
||||
echo " find $PICTURES_DIR -name '*-preview.webp' -exec du -ch {} + | tail -1"
|
||||
echo " find $PICTURES_DIR -name '*.jpg' -exec du -ch {} + | tail -1"
|
||||
Reference in New Issue
Block a user