Skip to content

Instantly share code, notes, and snippets.

@zoxon
Created August 5, 2026 05:28
Show Gist options
  • Select an option

  • Save zoxon/897ec364bad74eb2d12b1f2af300d4eb to your computer and use it in GitHub Desktop.

Select an option

Save zoxon/897ec364bad74eb2d12b1f2af300d4eb to your computer and use it in GitHub Desktop.
Convert images for web
#!/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"
# Create output folders
mkdir -p "$OUTPUT_DIR"
# PNG lossless
sharp -i "$INPUT_DIR/*.png" -o "$OUTPUT_DIR" \
--format png --lossless --effort 6
# JPEG remove artifacts
sharp -i "$INPUT_DIR/*.{jpg,jpeg}" -o "$OUTPUT_DIR" \
--format jpeg \
--quality 90 \
--trellisQuantisation \
--chromaSubsampling 4:4:4 \
--progressive
# JPEG -> AVIF + WEBP
sharp -i "$OUTPUT_DIR/*.{jpg,jpeg}" -o "$OUTPUT_DIR" \
--format avif --quality 75 --effort 6 --chromaSubsampling 4:4:4
sharp -i "$OUTPUT_DIR/*.{jpg,jpeg}" -o "$OUTPUT_DIR" \
--format webp --quality 80 --smartSubsample --nearLossless=70
# PNG → AVIF + WebP
sharp -i "$INPUT_DIR/*.png" -o "$OUTPUT_DIR" \
--format avif --quality 75 --effort 6 --chromaSubsampling 4:4:4
sharp -i "$INPUT_DIR/*.png" -o "$OUTPUT_DIR" \
--format webp --quality 80 --smartSubsample
echo "✓ All images processed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment