Skip to content

Instantly share code, notes, and snippets.

@webercoder
Last active December 30, 2020 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webercoder/c09b8ab201a093b68d6ed87ef81f4441 to your computer and use it in GitHub Desktop.
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.
#!/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