Skip to content

Instantly share code, notes, and snippets.

@dikaio
Created January 6, 2024 15:23
Show Gist options
  • Save dikaio/573ba6f02645b1ae7920c7d84583fa01 to your computer and use it in GitHub Desktop.
Save dikaio/573ba6f02645b1ae7920c7d84583fa01 to your computer and use it in GitHub Desktop.
Simple script to optimize images quality, size and format.

How to use

  1. Update the sizes and final format you want your images formatted to in source.
  2. make the script executable chmod +x optimize.sh
  3. run the script ./optimize.sh original.jpg
#!/bin/bash
# Check if an argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <filename.jpg>"
exit 1
fi
# Check if the provided file has a .jpg extension
if [[ ! $1 == *.jpg ]]; then
echo "The file must have a .jpg extension."
exit 1
fi
# The original filename without the extension
filename="${1%.*}"
# Array of desired widths
widths=(1536 1280 1024 768 640)
# Loop through the widths and create resized images
for width in "${widths[@]}"; do
convert "$1" -resize "${width}" -quality 80 -strip "${filename}@${width}.webp"
done
echo "Resizing and optimization completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment