Why your photos rotate when you upload them
Almost nothing rotates a photo when you turn the camera. The sensor is fixed in the body, so it records the same rectangle whichever way you hold it, and the camera writes a small orientation tag into the file to say how the result should be turned for viewing. Every program that shows you the picture then decides, on its own, whether to honour that tag. When two programs decide differently, one of them shows your photo sideways.
The camera does not turn the picture, it leaves a note
The EXIF metadata in a JPEG or HEIC file carries a field called Orientation, and it has eight defined values. One means the file is already the right way up. Three of the others are plain rotations by 90, 180 and 270 degrees, and the remaining four combine a rotation with a mirror flip.
So a portrait photo from a phone is usually a landscape image plus a note that says rotate this a quarter turn. The stored pixels are genuinely sideways, and every viewer that shows it upright is applying the tag as it draws.
Why one program obeys the note and another ignores it
Web browsers apply the tag when they display an ordinary image, and that behaviour is now the default. But anything that decodes the file down to raw pixels sees the sideways version, because at that point the tag is just a number that was left behind in the header.
That is why an upload can turn a photo: the site did not view your file, it re-encoded it — resized it, stripped its metadata, converted it to WebP — and whatever it wrote back has no note attached. The same happens in scripts. An image library rotates on request, not automatically, unless you explicitly ask it to apply the orientation first.
Rotating twice is a real failure mode
The opposite mistake is just as common. A tool turns the pixels the right way up but leaves the tag saying rotate a quarter turn, so the next viewer that honours the tag rotates an already-correct picture and you get a photo that is sideways the other way.
Whenever a photo comes out at ninety degrees from where it should be, the useful question is which of the two halves is wrong: the pixels or the note. They are separate pieces of information, and a file can be self-contradictory.
The width and height in the file are the sensor's, not the photo's
A portrait photo from a 12-megapixel phone reports itself as 4032 by 3024 — landscape. The dimensions in the header describe the stored pixels, and the orientation tag is what makes it portrait on screen.
Any layout that reserves space from those numbers, or any check that decides a picture is landscape before drawing it, will get portrait photos wrong. Galleries with gaps in odd places are usually this and not a CSS problem.
Mirrored selfies are the same mechanism
Four of the eight orientation values include a mirror flip, which exists because front cameras show you a mirrored preview. Some apps flip the pixels before saving; others save the sensor image and record the flip in the tag.
The visible symptom is text. A shirt logo or a whiteboard behind you reads backwards in one program and correctly in another, which tells you the flip is in the tag rather than in the pixels.
The thumbnail can disagree with the photo
EXIF can also hold a small embedded thumbnail, written by the camera when the picture was taken. File managers and photo apps often show that thumbnail because it is far quicker to read than the full image.
Editors do not always update it. A crop or a rotation that changes the picture but leaves the old thumbnail in place produces the odd situation where the preview and the opened file show different things — and, less obviously, a thumbnail that still contains part of a picture you thought you had cropped away.
How to make a photo that cannot be misread
Read the tag before you do anything, so you know whether you are looking at a rotation problem or a mirroring one, and whether the file claims to be rotated at all. A photo that is sideways with the tag already set to upright was rotated destructively by something upstream.
The durable fix is to bake the rotation into the pixels and then set the tag to upright or remove the metadata entirely, so no later program can rotate it again. Do that before uploading rather than after, because once a site has re-encoded and stripped the file, the note that would have told it what to do is gone.