Edit in JSFiddle

<div class="div">
  <img src="https://picsum.photos/300/600" alt="" class="img">
</div>

<div class="div">
  <img src="https://picsum.photos/500/300" alt="" class="img">
</div>
.div {
  width: 200px;
  height: 300px;
  margin: 20px;
  border: 1px solid black;
}

.img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}