When you say you "have to keep both width and height", what exactly do you want to happen?
If you want scaling as big as possible, treating the passed width and height as maximums, then see @Matt Jones's answet.
If you want the img to be cropped, you need to think through the scale of of the image within that cropped area, and apply imageResize() then imageCrop().