From what I can find there's no way to do this in pure CSS using an img tag. We could have js detect whether the image is wider or taller then add the appropriate CSS class to it:

    .fillwidth { width: 100%; height: auto; } 
    .fillheight { height: 100%; width: auto; }