It's 2017, and now you can use object-fit which has decent support. It works in the same way as a div's background-size
but on the element itself, and on any element including images.
.your-img {
max-width: 100%;
max-height: 100%;
object-fit: contain;
}