Skip to content

Instantly share code, notes, and snippets.

@DrSLDR DrSLDR/blog-resizer.sh
Last active Sep 20, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.