Last active
December 30, 2020 15:39
-
-
Save webercoder/c09b8ab201a093b68d6ed87ef81f4441 to your computer and use it in GitHub Desktop.
Create several sizes of an image for use with the HTML srcset attribute.
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 | |
function usage { | |
cat << EOM | |
Usage: $0 filename size [quality, default: 75] [count, default: 3] | |
For example: | |
$0 rad.jpg 1024 75 3 | |
Will create rad-1024.jpg, rad-2048.jpg, and rad-3072.jpg with 75% quality. | |
EOM | |
} | |
if [ "$#" -eq 0 ]; then | |
usage | |
exit 0 | |
elif [ "$#" -lt 2 ]; then | |
usage | |
exit 1 | |
fi | |
FILENAME=$1 | |
EXTENSION="${FILENAME##*.}" | |
SIZE=$2 | |
QUALITY=${3:-75} | |
COUNT=${4:-3} | |
for i in $(seq 1 $COUNT); do | |
sz=$(($SIZE * $i)) | |
CMD="convert \"$FILENAME\" -resize ${sz}x${sz} -quality $QUALITY \"${FILENAME%.$EXTENSION}-$sz.$EXTENSION\"" | |
echo Running $CMD... | |
eval $CMD | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment