This is mental. Use the scale-down property - it explains itself.
Inline styling:
This will stop flex from stretching it. In this case, the image would go to 50% of the width of its parent container and the height would scale down to match.
Keep it simple.