sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Resizing image results in rotation even with metadata on webp

Open paraslamsong opened this issue 3 years ago • 6 comments

I was resizing image with metadata

await sharp(buffer)
        .withMetadata()
        .resize(size)
        .webp()
        .toFile( filename));

This did resized and not rotated on the saved folder(may be OS read metadata) but on serving the browser displayed rotated image

Update: This issue is with webp and jpeg is working as intended

paraslamsong avatar Nov 25 '23 17:11 paraslamsong

I suspect this question relates to EXIF Orientation metadata and (lack of) support for it in software that renders WebP images. If you still require help, please can you provide a sample image that allows someone else to reproduce.

lovell avatar Nov 26 '23 10:11 lovell

I have the same problem in version 0.33.0 (@img/sharp-darwin-arm64)

The MimeType of the image result does not matter. The image is always rotated (png, avif, webp), some images are rotated by 180 degrees, others by 90 degrees.

My Code: return await sharp(buffer) .withMetadata() .resize({ width: 1440, height: 810, fit: 'cover', withoutEnlargement: true, position: 'centre' }) .avif() .toBuffer();

My Test-Image (will be rotated by 180 degrees): test

ChrisWun avatar Dec 08 '23 14:12 ChrisWun

@ChrisWun This input JPEG image contains an EXIF Orientation value of 3, which means the pixel values are already rotated 180 degrees (you might see it load from bottom to top in a web browser).

Support for EXIF Orientation in AVIF/HEIF images is a bit of a mess, e.g. the spec says it should be "descriptive" rather than used for presentation, so I generally recommend it is removed.

If you want to auto-orient before resizing (and remove the EXIF Orientation value), add .rotate() to your pipeline.

https://sharp.pixelplumbing.com/api-operation#rotate

lovell avatar Dec 08 '23 14:12 lovell

on serving the browser displayed rotated image

The orientation tag in WebP (stored within the EXIF chunk) is currently unsupported by any browser, see: https://zpl.fi/exif-orientation-in-different-formats/#webp

kleisauke avatar Dec 08 '23 14:12 kleisauke

Hey @lovell @kleisauke

Thank you very much for the quick reply. The inclusion of .rotate() has solved the problem for me.

Btw, sharp is a great library. Really appreciate your work!

ChrisWun avatar Dec 08 '23 14:12 ChrisWun

@paraslamsong I hope this information helped. Please feel free to re-open with more details if further assistance is required.

lovell avatar Dec 17 '23 20:12 lovell