IF statements ftw.
below is some code from an image resizing function i wrote in one of our sites - effectively does what you're after where args.height and args.width are the max values allowed. info.width and info.height are the existing dimensions of the image.
Code:
Please Login or Register to see this snippet
my suggestion would be something like that, but put ANOTHER if statement around it which is based on the mag level, and use that to set the args.height and args.width.
Obviously this is not the language you're working in, but the logic should be piss easy to follow.