notes.velouria.dev/scripts/compress_images.sh

26 lines
835 B
Bash
Executable File

#!/bin/bash
# Set the source and destination directories
SOURCE_DIR="content/images"
DESTINATION_DIR="public/images"
# Create the destination directory if it doesn't exist
mkdir -p "$DESTINATION_DIR"
# Iterate over image files in the source directory
for IMAGE_PATH in "$SOURCE_DIR"/*.jpg "$SOURCE_DIR"/*.png; do
# Check if there are matching files
if [ -e "$IMAGE_PATH" ]; then
# Extract the filename and extension
FILENAME=$(basename -- "$IMAGE_PATH")
EXTENSION="${FILENAME##*.}"
FILENAME_NOEXT="${FILENAME%.*}"
# Compress the image using `sharp` (ensure sharp is installed: npm install sharp)
npx sharp "$IMAGE_PATH" --webp --quality=75 > "$DESTINATION_DIR/$FILENAME_NOEXT.webp"
echo "Compressed $IMAGE_PATH to $DESTINATION_DIR/$FILENAME_NOEXT.webp"
fi
done
echo "Image compression complete!"