- Update the sizes and final format you want your images formatted to in source.
- make the script executable
chmod +x optimize.sh
- run the script
./optimize.sh original.jpg
Created
January 6, 2024 15:23
-
-
Save dikaio/573ba6f02645b1ae7920c7d84583fa01 to your computer and use it in GitHub Desktop.
Simple script to optimize images quality, size and format.
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 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