Call sharp's rotate() with no arguments (or autoOrient()), or Pillow's ImageOps.exif_transpose(), before any resize. A phone stores many photos sideways and adds an EXIF Orientation tag that says how to turn them. A resize that ignores the tag works on the stored pixels, then most encoders drop the tag, and your thumbnail comes out lying on its side. My test file came out 200 × 150 instead of 200 × 267.
What does the Orientation tag do?
A camera sensor has one fixed layout. When you hold the phone upright, the camera often writes the pixels in the sensor's landscape layout and sets EXIF Orientation to 6, meaning "turn 90° clockwise to show". Every viewer that honours the tag shows the photo upright. Your server code is not a viewer.
My test file is oriented.jpg from the Blinkimg engine's test fixtures: a 400 × 300 colour gradient stored with Orientation 6. Upright, it is 300 × 400. sharp sees both:
const sharp = require('sharp');
const m = await sharp('oriented.jpg').metadata();
console.log(m.width, m.height, 'orientation', m.orientation, 'autoOrient', m.autoOrient);
input 400 300 orientation 6 autoOrient { width: 300, height: 400 }
metadata().autoOrient gives the upright size. Use it, not width and height, when you compute layout boxes or aspect ratios from an upload.
What goes wrong in sharp?
I ran five pipelines, each to a 200 px wide JPEG at quality 80, and read the output back:
const f = 'oriented.jpg';
const runs = {
'resize only': sharp(f).resize({ width: 200 }),
'rotate() then resize': sharp(f).rotate().resize({ width: 200 }),
'resize then rotate() (same pipeline)': sharp(f).resize({ width: 200 }).rotate(),
'resize only + keepMetadata': sharp(f).resize({ width: 200 }).keepMetadata(),
'autoOrient option': sharp(f, { autoOrient: true }).resize({ width: 200 }),
};
for (const [k, p] of Object.entries(runs)) {
const out = await p.jpeg({ quality: 80 }).toBuffer();
const o = await sharp(out).metadata();
console.log(k.padEnd(38), `${o.width} x ${o.height}`, 'orientation', o.orientation ?? '-', out.length, 'bytes');
}
resize only 200 x 150 orientation - 3773 bytes
rotate() then resize 200 x 267 orientation - 6752 bytes
resize then rotate() (same pipeline) 200 x 267 orientation - 6752 bytes
resize only + keepMetadata 200 x 150 orientation 6 4461 bytes
autoOrient option 200 x 267 orientation - 6752 bytes
Resize only is the bug. sharp strips metadata by default, so the tag is gone, and the pixels are still sideways. The width of 200 also applied to the wrong side: the upright photo should be 200 wide and 267 tall.
keepMetadata() keeps the tag, so a viewer that honours it turns the 200 × 150 image upright, at 150 × 200. That is upright, but not the width you asked for, and any consumer that ignores EXIF (some email clients, a <canvas> without image-orientation) still shows it sideways.
The three correct runs give byte-identical output.
Note: Call order inside one sharp pipeline did not matter here.
resize().rotate()gave the same 6,752 bytes asrotate().resize(), because a no-argumentrotate()auto-orients at the start of the pipeline. The sharp operation docs still warn that "Method order is important when rotating, resizing and/or extracting regions", and that applies torotate(angle)with an explicit angle. Writerotate()first so the code reads in the order it runs.
The same docs say a rotate() with no angle calls autoOrient(), which will "Auto-orient based on the EXIF Orientation tag, then remove the tag". That removal is why the correct outputs show orientation -.
And in Pillow?
Pillow does not orient anything unless you ask. Image.thumbnail() works on the stored pixels:
import io
from PIL import Image, ImageOps
f = "oriented.jpg"
def save(im, **kw):
b = io.BytesIO(); im.save(b, "JPEG", quality=80, **kw)
back = Image.open(io.BytesIO(b.getvalue()))
return f"{back.width} x {back.height} orientation {back.getexif().get(0x0112, '-')} {len(b.getvalue())} bytes"
im = Image.open(f)
print("input", im.size, "orientation", im.getexif().get(0x0112))
a = Image.open(f); a.thumbnail((200, 200)); print("thumbnail only ", save(a))
b = Image.open(f); print("thumbnail, exif kept ", end=" "); b.thumbnail((200, 200)); print(save(b, exif=b.getexif()))
c = ImageOps.exif_transpose(Image.open(f)); c.thumbnail((200, 200)); print("exif_transpose then thumbnail", save(c, exif=c.getexif()))
input (400, 300) orientation 6
thumbnail only 200 x 150 orientation - 4400 bytes
thumbnail, exif kept 200 x 150 orientation 6 4590 bytes
exif_transpose then thumbnail 150 x 200 orientation - 4343 bytes
The pattern matches sharp. Without the transpose you get sideways pixels and no tag. With the tag copied back you get sideways pixels that only EXIF-aware viewers fix. With exif_transpose() first you get upright pixels and no tag.
Per the Pillow ImageOps docs, exif_transpose() transposes the image and removes the orientation data. By default it returns a new image; in_place=True changes the original and returns None, so do not assign that result.
Note the sizes: thumbnail((200, 200)) fits inside a box, so the upright result is 150 × 200. sharp's resize({ width: 200 }) fixes the width, so its result is 200 × 267. Both are right for what they were asked.
Where does this bite in a real pipeline?
Anywhere the code reads dimensions before orienting:
- Aspect-ratio checks. "Reject portrait images" rejects landscape ones and passes portrait ones.
-
widthandheightattributes. You emitwidth="400" height="300"for a photo the browser draws at 300 × 400, and the layout shifts. - Crops. A centre crop to a square takes the wrong band of the photo.
- srcset widths. You generate a 1600 px candidate from what is really a 1200 px wide image.
Orient once, at the top of the pipeline, and every step after it sees the photo the way a person does. If you just need a thumbnail and not the code, the JPEG compressor on our site applies the orientation before it resizes.
How I measured
- Machine: Apple M1 Pro, macOS 15.7.3, on 2026-10-17.
- Node 24.22.0, sharp 0.35.4, libvips 8.18.6.
- Python 3.9.6 in a venv, Pillow 11.3.0.
- File:
packages/engine/tests/fixtures/oriented.jpg, 400 × 300 stored, EXIF Orientation 6.
What I did not test
I tested Orientation 6 only, not the mirrored values (2, 4, 5, 7). I did not test HEIC input, where the rotation can live in the container instead of EXIF.
I build Blinkimg, an image compressor and converter with a developer API. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.

Top comments (0)