My photo gallery add-in will do this, without any need for programming -
http://www.gmayor.com/photo_gallery_template.html - it was designed for such an application.
If you want to roll your own, then use a suitably sized unbordered table to hold the images. Images that are larger than a table cell will adapt to the width of that cell.