Resizing image results in rotation even with metadata on webp
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
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.
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):
@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
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
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!
@paraslamsong I hope this information helped. Please feel free to re-open with more details if further assistance is required.