Check this codenpen. I added 2 ways of handling images, inline and as background.
You can change the values of fitting the images with the dropdowns.
Also on load there is a check whether the image is portrait or landscape. So if you want to go that way, it should also be possible.