Created
December 18, 2024 09:41
-
-
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
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 | |
# 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