EXIF Orientation: The 8 Values, Browser Behavior, and Fixes
A phone photo that looks upright in the gallery can arrive sideways in a PDF, an OCR result, or a canvas you drew it into. The pixels never moved — the file carries an EXIF orientation tag that says how those pixels must be rotated or mirrored for display, and whether the code that reads the file honours that tag decides what you get.
In current Chromium browsers the tag is applied for you: <img>, ctx.drawImage(), and createImageBitmap() all produced upright pixels for every one of the eight values in the test below. So the fix for a sideways image is almost never “rotate it by 90 degrees” — it is “find the step that dropped the tag, or the step that rotated a second time”.
What you’ll build: a reference for the eight EXIF orientation values, a measured table of what Chrome 156 and Edge 157 do with each one, and normalization code for JavaScript, Android, and iOS that produces an upright image with the tag no longer in play.
Key Takeaways
- EXIF orientation is a single number from 1 to 8. Values 2, 4, 5 and 7 mirror, values 3, 6 and 8 rotate, and value 1 means the stored pixels are already upright.
- Values 5 to 8 also swap width and height: a portrait phone capture is stored as a landscape buffer, which is why a raw read of a 12 MP photo reports 4000×3000 for a 3000×4000 picture.
- In Chrome 156 and Edge 157 on Windows 11,
<img>,ctx.drawImage()andcreateImageBitmap()applied the tag for all eight values, so the “canvas ignores EXIF” advice from the pre-2020 era no longer describes Chromium. image-orientation: nonein CSS changes painting only. It did not changenaturalWidth/naturalHeight, anddrawImage()kept drawing the oriented pixels.- Drawing through a canvas and exporting with
toBlob()produces upright pixels and a file with no orientation tag, which is exactly what an upload pipeline wants.
Common Developer Questions
What do the eight EXIF orientation values mean?
Each value describes the transform a viewer must apply to the stored pixels before showing them: 1 is identity, 2 mirrors horizontally, 3 rotates 180°, 4 mirrors vertically, 5 transposes across the main diagonal, 6 rotates 90° clockwise, 7 transposes across the anti-diagonal, and 8 rotates 270° clockwise. The full table is below.
Does canvas drawImage apply EXIF orientation?
Yes in Chrome 156 and Edge 157. Drawing an image whose tag was 6 (rotate 90° clockwise) into a canvas produced the upright pixels and the swapped 800×1200 canvas size, and the same happened for all eight values. Firefox and Safari were not measured for this article, so treat that as Chromium evidence.
How do I fix a sideways photo in JavaScript?
Draw the image into a canvas sized to the image’s naturalWidth and naturalHeight, then export it with toBlob(). Because the browser has already applied the tag, the exported pixels are upright and the tag is gone, so downstream code cannot rotate it a second time.
Why does a photo look right in the browser but sideways in my app?
The pipeline that produced the sideways copy either read raw pixels and ignored the tag, or applied the tag and then rotated again using the tag it also copied. Native decoders, some image libraries, server-side resizers, and PDF builders are the usual suspects; a canvas export removes the ambiguity because no tag survives it.
How do I read the orientation on Android and iOS?
On Android, pass the file path to ExifInterface and read TAG_ORIENTATION — a Bitmap does not carry the tag. On iOS, UIImage.imageOrientation already holds the value, and redrawing the image into a new graphics context bakes it into the pixels.
Prerequisites
To follow along with the code samples you will need:
- A modern browser for the JavaScript section (Chrome, Edge, Firefox, or Safari).
- Android: Android Studio with API level 24+ for
ExifInterface. - iOS: Xcode with a Swift 5+ project.
- Dynamsoft Document Normalizer (optional): used in the sample repos for document boundary detection and cropping. Get a 30-day free trial license to try it.
The Tag in One Picture
Every file below is the same upright card, stored eight different ways, each with the tag that puts it back upright. The left column is what is in the file; the right column is what a browser paints. All eight right-hand images are identical, which is the whole point of the tag.

A real capture behaves the same way. The scan below was stored as a landscape buffer, because that is how the sensor read it out, and carries orientation 6:

The Eight Values
The tag lives in the TIFF-style IFD of a JPEG or HEIC file as tag 0x0112 (274 in decimal). Its value maps to a transform, and to a stored size:
| Value | Name | Transform applied for display | Stored size for a portrait capture |
|---|---|---|---|
| 1 | Normal | none | 800×1200 |
| 2 | Mirror horizontal | flip left to right | 800×1200 |
| 3 | Rotate 180 | turn upside down | 800×1200 |
| 4 | Mirror vertical | flip top to bottom | 800×1200 |
| 5 | Transpose | mirror across the top-left / bottom-right diagonal | 1200×800 |
| 6 | Rotate 90 CW | rotate clockwise, the common portrait phone photo | 1200×800 |
| 7 | Transverse | mirror across the top-right / bottom-left diagonal | 1200×800 |
| 8 | Rotate 270 CW | rotate counter-clockwise | 1200×800 |
Two consequences matter in code:
- Values 2, 4, 5 and 7 mirror the image. Text becomes unreadable in the mirror image, so OCR on a front-camera capture of a document needs the mirrored cases, not just the three rotations. A
switchwith only 3, 6 and 8 silently passes values 2, 4, 5 and 7 through untouched. - Values 5 to 8 swap the axes. Any layout, crop rectangle, or bounding box computed from a raw read is transposed relative to what the user sees, which is why document boundary detection sometimes returns a quad that looks rotated by 90°.
If you need the CSS equivalent for a preview, the unambiguous ones are scaleX(-1) for 2, rotate(180deg) for 3, scaleY(-1) for 4, rotate(90deg) for 6 and rotate(270deg) for 8.
What Browsers Actually Do With the Tag
The measurements below come from eight JPEG fixtures that share one 800×1200 upright card, each stored and tagged so that a correct viewer shows the same upright card. Each tagged file also has a tag-free twin with identical pixels, so “the browser applied the tag” is a comparison rather than an assumption.
Environment: Windows 11, Chrome 156.0.8078.81 and Edge 157.0.4341, headless, files served over http://127.0.0.1, tags 1–8. Both browsers returned identical results.
| What you ask for | Tag 6 (stored 1200×800, needs a 90° clockwise turn) |
|---|---|
<img> paint |
upright |
img.naturalWidth × naturalHeight |
800×1200 |
<img style="image-orientation: none"> paint |
the raw 1200×800 buffer, content sideways |
naturalWidth with image-orientation: none |
800×1200, unchanged |
ctx.drawImage(img, 0, 0) |
upright pixels, so the canvas must be 800×1200 |
createImageBitmap(blob) |
800×1200, upright |
createImageBitmap(blob, { imageOrientation: "none" }) |
800×1200, upright |
canvas.toDataURL("image/jpeg") and re-read |
800×1200, no tag left |

Reading that table:
- Painting and pixel access agree. The
<img>element,drawImage()andcreateImageBitmap()all hand you upright pixels, so code that mixes a preview with a canvas crop does not need separate correction. - CSS is a paint-time switch, not a data switch.
image-orientation: noneshows the raw buffer, which is useful for a “show me the original” toggle, butnaturalWidthanddrawImage()still behave as if the tag applied. Do not use it hoping to get unrotated pixel data. imageOrientation: "none"did not undo the rotation in these builds: the bitmap still came back 800×1200 and upright, whileimageOrientation: "flipY"did visibly flip the result, so the option is parsed and this one value is simply not honoured. If you need the stored buffer exactly as written, read the tag and invert the transform yourself.- A canvas export is a normalizer. The exported JPEG came back at 800×1200 with no orientation tag, because
toDataURL()andtoBlob()write new pixel data and no EXIF. That is the cheapest way to make an image’s orientation unambiguous for everything downstream.
Firefox and Safari were not measured here, and mobile browsers were not measured either; the table is Chromium evidence from two builds.
Normalize Orientation in JavaScript
Because the browser has already done the rotation, normalization is a resize-free redraw. Use naturalWidth / naturalHeight — the oriented dimensions — not the CSS size:
async function normalizeOrientation(file) {
const bitmap = await createImageBitmap(file); // already upright in Chromium
const canvas = document.createElement("canvas");
canvas.width = bitmap.width;
canvas.height = bitmap.height;
canvas.getContext("2d").drawImage(bitmap, 0, 0);
bitmap.close();
return new Promise((resolve, reject) => {
canvas.toBlob(
(blob) => (blob ? resolve(blob) : reject(new Error("encode failed"))),
"image/jpeg",
0.92
);
});
}
Swap createImageBitmap for an <img> element when you need to support older Safari, and call the function after img.decode() resolves so naturalWidth is populated:
const img = new Image();
img.src = URL.createObjectURL(file);
await img.decode();
const canvas = document.createElement("canvas");
canvas.width = img.naturalWidth; // oriented width
canvas.height = img.naturalHeight; // oriented height
canvas.getContext("2d").drawImage(img, 0, 0);
That is the whole fix for a browser upload path. The next snippet is for the case where something else hands you raw, unrotated pixels — a decoder in a worker, a native bridge, a library that returns buffers — and you have to apply the tag yourself:
function drawWithOrientation(ctx, source, orientation, width, height) {
const swap = orientation >= 5 && orientation <= 8;
ctx.canvas.width = swap ? height : width;
ctx.canvas.height = swap ? width : height;
switch (orientation) {
case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;
case 3: ctx.transform(-1, 0, 0, -1, width, height); break;
case 4: ctx.transform(1, 0, 0, -1, 0, height); break;
case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;
case 6: ctx.transform(0, 1, -1, 0, height, 0); break;
case 7: ctx.transform(0, -1, -1, 0, height, width); break;
case 8: ctx.transform(0, -1, 1, 0, 0, width); break;
}
ctx.drawImage(source, 0, 0, width, height);
}
Run against the same eight fixtures, this function produced output identical to the browser’s own rendering for all eight values, so the matrices are correct rather than approximate. Do not run it on an image the browser has already oriented — the result is the double rotation described below.
Correct Orientation with EXIF on Android and iOS
The same job on mobile, where the decoder usually hands you pixels and the tag separately.
For Android (Java), ExifInterface needs the file path, because a decoded Bitmap has no EXIF block:
private Bitmap rotatedImageBasedOnExif(Bitmap bitmap, String path) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
ExifInterface exif = null;
try {
exif = new ExifInterface(path);
} catch (IOException e) {
return bitmap;
}
int rotate = 0;
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
try (FileOutputStream out = new FileOutputStream(path)) {
rotated.compress(Bitmap.CompressFormat.JPEG, 100, out);
} catch (IOException e) {
e.printStackTrace();
}
return rotated;
}
return bitmap;
}
For iOS (Swift), UIImage.imageOrientation already carries the flag, and redrawing through a graphics context rewrites the pixels so the value becomes .up:
static func normalizedImage(_ image:UIImage) -> UIImage {
if image.imageOrientation == UIImage.Orientation.up {
return image
}
UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
image.draw(in: CGRect(x:0,y:0,width:image.size.width,height:image.size.height))
let normalized = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
return normalized
}
Both samples come from Dynamsoft Document Normalizer based scanners, where the corrected image is then passed to boundary detection.
Where Sideways Pages Come From in Document Capture
In a capture and OCR pipeline, orientation errors cluster in four places:
- The decode step. Anything that reads raw pixels — a native bridge, a WASM decoder, a server-side resizer — gets the stored buffer and must apply the tag itself. A JPEG that reports 4000×3000 for a 3000×4000 page is the tell.
- The scanner, not the camera. Sheet-fed scanners put pages in face-up or face-down in whatever direction the operator fed them, so the problem is 180° flips and mixed stacks rather than a single tag. That is a separate problem with its own fixes: auto-correcting scanned page orientation covers TWAIN’s
ICAP_AUTOMATICROTATEcapability and OCR-based detection, and deskewing a scanned page handles the few degrees of tilt that remain. - Manual correction in the viewer. Users rotate a page that is already upright, or your code rotates on top of an oriented decode. Both produce the same symptom: a page that is 90° off in exactly one place in the flow. Rotating a page interactively is a display operation — see rotating images with JavaScript for the CSS and canvas split, and flipping or mirroring for the mirror axes that values 2, 4, 5 and 7 map onto.
- The upload step. If a client normalizes but the server re-reads the original file, nothing changed. Normalize on the side that owns the bytes, and send the canvas export rather than the original.
Common Issues & Edge Cases
- Double rotation. The most common bug in this area: the browser applies the tag, then the code rotates by the tag value as well, and the page ends up mirrored or 90° off. If your output is wrong by exactly the tag’s angle, look for two corrections rather than one.
- Re-encoding drops the tag but keeps the pixels. Resizing a JPEG without applying the tag gives you a sideways file that no longer announces why. Apply the transform before any re-encode.
naturalWidthis the oriented size in Chromium. Layout math, crop rectangles and DPI calculations all need the oriented value; a raw read gives the transposed one for values 5–8.- Mirrored flags are rare but real. Values 2, 4, 5 and 7 come from front-facing cameras and some scanners. The Android snippet above handles the three rotations only; add
Matrix.preScale(-1, 1)(and the transposed variants) if you need the mirror cases, or use the JavaScript matrix table as the reference. - HEIF and HEIC store orientation differently. Apple’s container is not a JPEG APP1 block, so an EXIF reader written for JPEG may return nothing. Test the HEIC path separately if your app accepts those files.
image-orientation: noneis a preview tool. It is the right way to show “what is actually in the file”, and the wrong way to try to normalize it.
Source Code
Check out the Android and iOS document scanner demos to have a try:
- Get the complete sample project source code on GitHub
- Get the complete sample project source code on GitHub
The value names in this article follow the EXIF specification; the archived Exif orientation reference is a useful cross-check for the mirrored cases.