#!/bin/bash
desiredHeight=800
desiredWidth=1200
resize_image () {
	imageFile=$1
	imageHeight="$(identify -format '%h' $imageFile)"
	imageWidth="$(identify -format '%w' $imageFile)"
	heightRatio=$(echo "scale=5;$desiredHeight/$imageHeight" | bc)
	widthRatio=$(echo "scale=5;$desiredWidth/$imageWidth" | bc)
	shrinkRatio=0
	if [ 1 -eq "$(echo "${heightRatio} < 1" | bc)" ] && [ 1 -eq "$(echo "${widthRatio} < 1" | bc)" ]
	then
		if [ 1 -eq "$(echo "${heightRatio} > ${widthRatio}" | bc)" ]
		then
			convert $imageFile -resize $(echo "$heightRatio*100/1" | bc)% $imageFile
			echo "height"
		else
			convert $imageFile -resize $(echo "$widthRatio*100/1" | bc)% $imageFile
			echo "width"
		fi
	fi
}

for f in `find . -name '*.jpg' -or -name '*.png'`;
do resize_image "$f"; done
