Since the container has fixed width/height you can do like below:
.photo-wrapper {
--w: 200px;
--h: 240px;
width: var(--w);
height: var(--h);
border: 1px solid;
display:inline-block;
}
img {
display: block;
width: var(--w);
height: var(--h);
object-fit:cover;
}
img.rotate {
transform: rotate(90deg) translateY(-100%);
width: var(--h);
height: var(--w);
transform-origin: top left;
}
<div class="photo-wrapper">
<img src="https://picsum.photos/id/1/400/300">
</div>
<div class="photo-wrapper">
<img class="rotate" src="https://picsum.photos/id/1/400/300">
</div>
<div class="photo-wrapper">
<img src="https://picsum.photos/id/104/500/600">
</div>
<div class="photo-wrapper">
<img class="rotate" src="https://picsum.photos/id/104/500/600">
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…