Scaling a centered image to longest dimension

OK, I am attempting to create an image gallery like this one…

Basically I would like the loaded jpg to center and scale to it’s longest side within the stage. So a portrait shot will stretch 100% from top to bottom but might have bars on either side. A landscape shot might letterbox if it is short enough. However, if the landscape shot is too tall, the top and bottom would bleed off the screen.

Make sense?