Skip to content

Instantly share code, notes, and snippets.

@zoxon
Created December 18, 2024 09:41
Show Gist options
  • Save zoxon/706e73352f3b25a530fdd6d07ea95f80 to your computer and use it in GitHub Desktop.
Save zoxon/706e73352f3b25a530fdd6d07ea95f80 to your computer and use it in GitHub Desktop.
Optimize png and jpg and convert it to webp and avif, using node sharp
#!/bin/bash
# Check if sharp-cli is installed
if ! command -v sharp &> /dev/null; then
echo "Error: sharp-cli is not installed. Please install it using 'npm install -g sharp-cli'."
exit 1
fi
# Directories
INPUT_DIR="."
OUTPUT_DIR="./output"
# Step 0: Create output folders
mkdir -p "$OUTPUT_DIR"
# Step 1: Optimize images
sharp -i "$INPUT_DIR/*.{png,jpg,jpeg}" -o "$OUTPUT_DIR" --format png --effort 2
# Step 2: Convert optimized image to WebP
sharp -i "$OUTPUT_DIR/*.{png,jpg,jpeg}" -o "$OUTPUT_DIR" --format webp --quality 85 --smartSubsample
# Step 3: Convert optimized image to AVIF
sharp -i "$OUTPUT_DIR/*.{png,jpg,jpeg}" -o "$OUTPUT_DIR" --format avif --quality 60 --effort 6
echo "All images processed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment