Created
December 24, 2025 18:45
-
-
Save clemsau/379833078d162bfd076218348367a9bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Script to optimize images in blog posts and projects | |
| # Converts images to WebP format with max width of 1200px | |
| # Updates references in text files automatically | |
| # Check if required tools are installed | |
| if ! command -v cwebp &> /dev/null; then | |
| echo "Error: cwebp is not installed. Please install webp tools:" | |
| echo "Ubuntu: sudo apt install webp" | |
| echo "Mac: brew install webp" | |
| exit 1 | |
| fi | |
| # Configuration | |
| MAX_WIDTH=1200 | |
| QUALITY=85 | |
| CONTENT_DIR="content" | |
| # Supported image extensions | |
| IMAGE_EXTENSIONS=("jpg" "jpeg" "png" "JPG" "JPEG" "PNG") | |
| # Supported text file extensions for reference updates | |
| TEXT_EXTENSIONS=("md" "txt" "html" "htm") | |
| # Function to process images in a directory | |
| process_directory() { | |
| local dir="$1" | |
| local dir_type="$2" | |
| echo "Processing $dir_type: $dir" | |
| # Find all image files in the directory | |
| local images_found=0 | |
| local images_processed=0 | |
| for ext in "${IMAGE_EXTENSIONS[@]}"; do | |
| for image_file in "$dir"/*."$ext"; do | |
| # Check if file actually exists (glob might not match anything) | |
| if [[ -f "$image_file" ]]; then | |
| images_found=$((images_found + 1)) | |
| # Get filename without extension | |
| local filename=$(basename "$image_file") | |
| local name_without_ext="${filename%.*}" | |
| local webp_file="$dir/$name_without_ext.webp" | |
| echo " Converting: $filename" | |
| # Convert to WebP with resize | |
| if cwebp -resize "$MAX_WIDTH" 0 -q "$QUALITY" "$image_file" -o "$webp_file"; then | |
| echo " ✓ Created: $name_without_ext.webp" | |
| # Update references in text files | |
| update_references "$dir" "$filename" "$name_without_ext.webp" | |
| # Remove original image | |
| rm "$image_file" | |
| echo " ✓ Removed: $filename" | |
| images_processed=$((images_processed + 1)) | |
| else | |
| echo " ✗ Failed to convert $filename to WebP" | |
| fi | |
| fi | |
| done | |
| done | |
| if [[ $images_found -eq 0 ]]; then | |
| echo " No images found in $dir" | |
| else | |
| echo " Processed $images_processed/$images_found images in $dir" | |
| fi | |
| echo | |
| } | |
| # Function to update image references in text files | |
| update_references() { | |
| local dir="$1" | |
| local old_filename="$2" | |
| local new_filename="$3" | |
| echo " Updating references: $old_filename → $new_filename" | |
| local files_updated=0 | |
| for ext in "${TEXT_EXTENSIONS[@]}"; do | |
| for text_file in "$dir"/*."$ext"; do | |
| if [[ -f "$text_file" ]]; then | |
| # Use sed to replace the filename (case-insensitive) | |
| # This handles various markdown/html image syntaxes | |
| if sed -i.bak "s|$old_filename|$new_filename|g" "$text_file" 2>/dev/null; then | |
| # Check if any changes were made | |
| if ! diff -q "$text_file" "$text_file.bak" >/dev/null 2>&1; then | |
| echo " ✓ Updated references in $(basename "$text_file")" | |
| files_updated=$((files_updated + 1)) | |
| fi | |
| # Remove backup file | |
| rm -f "$text_file.bak" | |
| fi | |
| fi | |
| done | |
| done | |
| if [[ $files_updated -eq 0 ]]; then | |
| echo " No text files needed updating" | |
| fi | |
| } | |
| # Main script execution | |
| echo "Blog Image Optimizer" | |
| echo "====================" | |
| echo "Max width: ${MAX_WIDTH}px" | |
| echo "Quality: ${QUALITY}%" | |
| echo "Content directory: $CONTENT_DIR" | |
| echo | |
| # Check if content directory exists | |
| if [[ ! -d "$CONTENT_DIR" ]]; then | |
| echo "Error: Content directory '$CONTENT_DIR' not found!" | |
| echo "Make sure you're running this script from your project root." | |
| exit 1 | |
| fi | |
| total_dirs=0 | |
| processed_dirs=0 | |
| # Process posts | |
| if [[ -d "$CONTENT_DIR/posts" ]]; then | |
| echo "=== PROCESSING POSTS ===" | |
| for post_dir in "$CONTENT_DIR/posts"/*/; do | |
| if [[ -d "$post_dir" ]]; then | |
| total_dirs=$((total_dirs + 1)) | |
| process_directory "$post_dir" "post" | |
| processed_dirs=$((processed_dirs + 1)) | |
| fi | |
| done | |
| else | |
| echo "No posts directory found at $CONTENT_DIR/posts" | |
| fi | |
| # Process projects | |
| if [[ -d "$CONTENT_DIR/projects" ]]; then | |
| echo "=== PROCESSING PROJECTS ===" | |
| for project_dir in "$CONTENT_DIR/projects"/*/; do | |
| if [[ -d "$project_dir" ]]; then | |
| total_dirs=$((total_dirs + 1)) | |
| process_directory "$project_dir" "project" | |
| processed_dirs=$((processed_dirs + 1)) | |
| fi | |
| done | |
| else | |
| echo "No projects directory found at $CONTENT_DIR/projects" | |
| fi | |
| # Summary | |
| echo "=== SUMMARY ===" | |
| echo "Processed $processed_dirs directories" | |
| echo "Optimization complete!" | |
| echo | |
| echo "Note: Original images have been replaced with optimized WebP versions." | |
| echo "References in .md, .txt, .html, and .htm files have been updated automatically." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment