Skip to content

Instantly share code, notes, and snippets.

@DrSLDR
Last active September 20, 2015 20:53
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 DrSLDR/3d73847210bec48aa843 to your computer and use it in GitHub Desktop.
Save DrSLDR/3d73847210bec48aa843 to your computer and use it in GitHub Desktop.
A script I use to (bulk) resize images to a largest dimension of 1080px, which I use for my blog.
#!/bin/bash
#Fundamentals
ALLMODE=0
# Get parameter
if [ -z "$1" ]; then
echo Filename or -all flag needed
exit 1
elif [ "$1" == "-all" ]; then
ALLMODE=1
echo "Blog-resizer - resizing all images in directory"
else
NAME=$1
fi
# Set up filename array
if [ $ALLMODE == 1 ]; then
names=$(ls)
else
names=( $NAME )
fi
# Loop over files
for f in $names
do
# File existence test
if [ ! -f "$f" ]; then
echo "File $f does not exist or is not a file"
exit 1
fi
# Split the input name into an array
IFS="." read -a splitname <<< "${f}"
# Set operational variables
newname=""
num=${#splitname[@]}
let num-=1
length=$num
# Get name of new file
for l in ${splitname[@]}
do
if [ $num == 0 ]; then
newname+="_e"
fi
if [ $num != $length ]; then
newname+="."
fi
newname+="$l"
let num-=1
done
# Tell the user
echo "Resizing $f, as $newname (1080x1080)"
# Resize
convert $f -resize 1080x1080 $newname
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment