Resize dynamic jpg from xml, in max size

What I want to do is load external jpgs and display them within a predifined box. I use the Kirupa’s code for the image loading part ( http://www.kirupa.com/developer/mx2004/xml_flash_photogallery.htm )

But the original images are larger than the display box. I want to rescale them with actionscript, and keep the size ratio of it. But the original pictures don’t have the same size.

Does someone has a mathematical equation (or a link) to solve this problem? :worried:

Thanks