images
Why Your Photo Turns Sideways After Upload (EXIF Orientation)
Phone photos are often stored sideways with a tag saying how to turn them. Drop the tag and the photo goes sideways; rotate it but keep the tag and it turns twice.
By Kyle Hurst ·
The photo is upright on your phone, upright in the upload preview, and sideways once the site has it. Or you rotated it by hand to fix that, and now it is sideways the other way. A phone photo is often not stored upright: the camera saves the pixels the way the sensor was held, plus a small EXIF tag that says how to turn them for display. A photo goes sideways when some step keeps the pixels but drops the tag, and it double-rotates when some step turns the pixels but keeps the tag. We measured which common tools do which.
If you just need the photo fixed, make a copy whose pixels are already upright and whose tag is gone or set to 1. Any tool that “auto-orients” does that (commands below). To see which case you have with a JPEG, drop the original into our Image Metadata viewer and read the Orientation row. A value of 1, or no Orientation row at all, means the pixels are already upright. Anything from 2 to 8 means the file depends on the tag. (The viewer also reads EXIF from HEIC, AVIF, PNG and WebP files. HEIC and AVIF files are turned for display by a rotation stored in the container itself, which the viewer does not show and which can disagree with the EXIF tag, so for those two formats the row is a hint, not the answer.)
What is actually in the file
The tag is Orientation, tag number 274 (0x0112), a single SHORT that defaults to 1. The Exif standard (CIPA DC-008, Exif 2.3) defines it in terms of rows and columns: value 6, for example, means “the 0th row is the visual right-hand side of the image, and the 0th column is the visual top.” In plain terms: the stored image is lying on its side, and a viewer has to turn it 90° clockwise.
To test this we made eight small JPEGs, one per value, of the same picture: 64×48, landscape, with red, green, blue and yellow quadrants (red top-left). Each file stores its pixels so that applying its own tag produces the upright picture. We checked every stored file against the spec’s row and column wording.
| Value | Stored row 0 is the visual… | Stored column 0 is the visual… | To display, the viewer… | Stored size |
|---|---|---|---|---|
| 1 | top | left | does nothing | 64×48 |
| 2 | top | right | flips left-right | 64×48 |
| 3 | bottom | right | rotates 180° | 64×48 |
| 4 | bottom | left | flips top-bottom | 64×48 |
| 5 | left | top | transposes (flips across the top-left to bottom-right diagonal) | 48×64 |
| 6 | right | top | rotates 90° clockwise | 48×64 |
| 7 | right | bottom | transverses (flips across the other diagonal) | 48×64 |
| 8 | left | bottom | rotates 90° counter-clockwise | 48×64 |
Two things in that table cause most bugs. For values 5 to 8 the stored width and height are swapped, so any code that reads the dimensions straight from the pixels gets a portrait size for a landscape photo. And values 2, 4, 5 and 7 involve a mirror flip, so code that only handles rotations gets those four wrong.
The two ways a photo breaks
A photo’s trip to a web page is a chain of steps: the phone, maybe an editor, the upload form, a server-side resizer, a CDN, the browser. It only stays right if every step that rotates the pixels also removes the tag, and every step that does not rotate them keeps it. So there are exactly two failures:
- Sideways: a step drops the tag without rotating the pixels. Resizers and metadata strippers that throw away EXIF do this. Nothing tells the browser to turn the pixels any more. (Strictly, values 3, 2 and 4 come out upside down or mirrored rather than sideways; we call them all “sideways” below.)
- Double-rotated: a step rotates the pixels and keeps the tag. Now the pixels are upright, but the next viewer reads the old tag and turns them again.
We ran our eight files through the image tools on our Mac (macOS 27.0) and checked each output’s raw pixels and tag. “Correct” means a viewer that honors the tag shows it upright. No ImageMagick CLI was installed, so we used its official WebAssembly build.
| Tool and call | Pixels | Tag | Result for values 2–8 |
|---|---|---|---|
Pillow 13.0 Image.open().save() | unchanged | dropped | sideways (all 7) |
Pillow thumbnail() then save() | unchanged | dropped | sideways (all 7) |
Pillow save(exif=im.info['exif']) | unchanged | kept | correct |
Pillow ImageOps.exif_transpose() then save() | rotated | removed | correct |
Pillow exif_transpose(), then save with the original EXIF | rotated | kept | double-rotated (all 7) |
sharp 0.35.5 .resize() | unchanged | dropped | sideways (all 7) |
sharp .rotate().resize() or .autoOrient().resize() | rotated | removed | correct |
sharp .resize().keepMetadata() | unchanged | kept | correct |
| ImageMagick 7.1.2 (WebAssembly build) resize | unchanged | kept | correct |
| ImageMagick resize + strip | unchanged | dropped | sideways (all 7) |
| ImageMagick auto-orient + resize | rotated | set to 1 | correct |
macOS sips -Z 32 (sips-316) | unchanged | kept | correct |
macOS sips -r 90 | rotated | kept | see below |
Value 1 came out right in every row. Every failure is one of the two patterns, and the default calls are the dangerous ones: Pillow’s plain save() writes no EXIF unless you pass it, and sharp’s docs say plainly that “by default all metadata will be removed, which includes EXIF-based orientation” (sharp docs). A server that resizes uploads with either library’s defaults turns every photo tagged 2 to 8 sideways.
The sips -r 90 row is the “I fixed it by hand” trap: it turns the stored pixels and leaves the tag alone. Say an app that ignores the tag showed the value 6 photo sideways, so you rotated it 90° clockwise. The pixels are now upright and that app shows it correctly, but the file still says 6, so every viewer that honors the tag (every browser, for a start) turns it another 90°. It now looks right where it was wrong, and wrong everywhere else. The mirrored values fare worse. Rotating the stored pixels and then applying a flip is not the same as rotating the displayed picture. In tag-honoring viewers, values 1, 3, 6 and 8 got the 90° clockwise turn that was asked for, and values 2, 4, 5 and 7 turned 90° counter-clockwise instead.
This happens in real software. WordPress ticket #37140 is the double-rotation case in the wild: rotating an image in the media editor with Imagick left “the orientation of the new rotated image is still 8.” It was closed as fixed in the 4.7.3 milestone, with patches that reset the tag when an image is rotated. Years later, after WordPress started rotating uploads to match the tag, ticket #54937 found that images with values 2, 4, 5 and 7 still displayed wrong, because the flip function was called with its horizontal and vertical arguments swapped. That fix shipped in 6.1.
Why the browser preview looks right
Browsers apply the tag by default, which is why your upload preview is upright even when the server’s copy is not. The CSS image-orientation property has an initial value of from-image, defined in CSS Images 3 as “the angle that the metadata specifies is necessary to correctly orient the image.” It applies to <img> and to CSS background images alike. MDN lists the property as available across browsers since April 2020. Chrome first shipped the property in Chrome 81 with from-image as its initial value. Firefox had supported it since Firefox 26 but switched its initial value to from-image in early 2020 (Mozilla bug 1607667).
For scripts, the HTML spec says naturalWidth and naturalHeight “reflect the dimensions after applying any rotation needed to correctly orient the image, regardless of the value of the ‘image-orientation’ property” (HTML, the img element). In our test page in Chrome 157 on macOS, all eight files reported naturalWidth×naturalHeight of 64×48, including the four stored as 48×64.
(image-orientation: none turns this off, but MDN notes it “does not override the orientation of non-secure-origin images.”) So the browser hides the problem, and everything outside it (Python, Node, ImageMagick, a CDN’s resizer) can expose it. When a photo looks fine in the upload preview and wrong after processing, the processing step dropped the tag.
Client-side resizers and the canvas
Many upload forms resize in the browser by drawing the photo onto a <canvas> and exporting it. Whether that keeps the orientation depends on how the image gets onto the canvas. We tested each path with the eight files in Chrome 157 on macOS. We did not test Safari.
drawImage()of an<img>: the HTML spec says the image’s natural dimensions are already oriented. In Chrome all eight files drew upright.createImageBitmap(): the HTML spec gives theimageOrientationoption two values,"from-image"(the default) and"flipY". In Chrome, bitmaps made from the file’s Blob and from an<img>were upright for all eight values with the default options and with"from-image", and"flipY"flipped them vertically.- The export: a canvas export is a new image encoded from the canvas pixels. In Chrome, a
toBlob('image/jpeg')round trip came out upright with no Exif block at all, so the tag is gone either way.
One trap: MDN still lists an imageOrientation: 'none' that ignores the metadata. The spec now says “There used to be a “none” enum value. It was renamed to “from-image”. In the future, “none” will be added back with a different meaning.” Chrome 157 still accepts 'none' and treats it as 'from-image': the bitmaps came out upright. You cannot use it to get the raw stored pixels.
Safari is less certain. WebKit bug 237895, “createImageBitmap() from Blob loses exif orientation,” was filed in March 2022 against Safari 15. Its reporter said a bitmap made from an <img> element was oriented correctly but one made from a Blob was not. A May 2024 comment on the bug says all browsers now render the test case the same way, but the bug is still open, so we can’t say whether current Safari is affected.
Put those together and a canvas resizer can produce a sideways image (if the decode ignored the tag) but should never produce a double-rotated one (because no tag survives). Our Image Resizer works this way: it decodes the file with createImageBitmap(file) using the default options, draws the bitmap onto a canvas at your chosen size, and exports with toBlob(). In Chrome that bakes the rotation into the pixels and leaves no tag, which also makes it a quick way to fix a photo before uploading it somewhere that strips metadata. If a browser’s Blob decode ignores the tag, as bug 237895 describes for older Safari, the output would be sideways.
If you build the upload form
The rule that prevents both failures: apply the orientation once, at the start of your pipeline, and remove the tag in the same step. After that, every later step sees upright pixels and no tag, so there is nothing left to misread.
// Node, sharp: rotate() with no angle auto-orients and removes the tag
await sharp(input).rotate().resize(1600).jpeg().toFile(out);
# Python, Pillow: exif_transpose rotates and removes the Orientation tag
from PIL import Image, ImageOps
im = ImageOps.exif_transpose(Image.open(path))
im.save(out) # don't copy the original file's EXIF onto it
# ImageMagick: -auto-orient rotates and sets the tag to 1 (top-left)
magick input.jpg -auto-orient -resize 1600x1600 out.jpg
Pillow’s docs say exif_transpose will “transpose the image accordingly, and remove the orientation data”, and sharp’s docs say autoOrient() orients “based on the EXIF Orientation tag, then remove[s] the tag.” The sharp and Pillow calls gave correct output for all eight values in our runs, as did ImageMagick’s autoOrient() in its WebAssembly build (we did not run the magick command line). The Pillow comment is the line people get wrong: copying the original EXIF onto the transposed image (to keep the capture date, say) brings the old Orientation back. That was the double-rotation row in our table.
The rest of the checklist:
- Read dimensions after orienting. In sharp,
metadata()on our value 6 file returnedwidth: 48, height: 64, orientation: 6. The upright size was only in itsautoOrientfield (64×48). Pillow’sim.sizeis also the stored size. If you validate aspect ratios or pick crop boxes from these numbers, orient first. - If you strip metadata for privacy, rotate first. Stripping EXIF from an un-rotated photo is the sideways case by definition.
- Test all eight values, not just 6. WordPress’s flip bug hid in 2, 4, 5 and 7. Eight files with four colored quadrants are enough: if red is top-left and the image is landscape, it is right.
How our tools handle orientation
Our EXIF Remover edits the file at the byte level and never re-encodes the pixels, so it cannot rotate them. Dropping the whole EXIF block would turn every rotated JPEG sideways, so when a JPEG’s Orientation is anything other than 1, it writes back a tiny EXIF block that holds only that one tag. We ran it on all eight test files. GPS was detected in every input and in none of the outputs, values 2 to 8 kept their tag, and value 1 lost its EXIF entirely, which is harmless because 1 is the default. All eight outputs display correctly. PNG and WebP files get the same treatment: a rotated file keeps an eXIf (PNG) or EXIF (WebP) chunk that holds only the Orientation tag. A PNG we made with sips from the value 6 photo carried its orientation in an eXIf chunk; after stripping, that chunk held Orientation 6 and nothing else, and the GPS was gone. Whether the tag does anything depends on the viewer. In Chrome 157 on macOS, PNG files with Orientation 2 to 8 in an eXIf chunk displayed upright, before and after stripping, while WebP files with the same tag in an EXIF chunk displayed as stored, tag ignored, both before and after. So for WebP the kept tag only helps viewers that read it. One case is not covered: an older PNG that keeps its EXIF in a text chunk (ImageMagick’s “Raw profile type exif”) loses that chunk whole, orientation included. Why stripping matters at all is covered in does your photo leak your location?.
Our Image Metadata viewer shows the tag as a number plus a label, such as 6 (Rotated 90° CW) or 5 (Mirrored horizontally, rotated 270° CW). The labels follow ExifTool’s naming (“Rotate 90 CW”) and describe the turn a viewer applies, not the way the stored pixels lie (for value 6 those are turned the opposite way). The width and height it shows come from the browser’s decoded bitmap, so they are the oriented size wherever the browser applies the tag on decode, as Chrome does.
One last case: if the original has value 1 (or no tag) and still looks wrong, the tag is not the problem. The pixels themselves are sideways, and the fix is an actual rotation.