Yet another refactoring of resizeImage

I hope all bugs are dead this time!