-
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some images get rotated for some reason #700
Comments
Can you confirm this also happens with |
Sorry for my late reponse, will take a look into this in a few weeks. Life's busy atm |
I'm running into the same issue and the problem is reproducible with the I've attached an archive of the jpg that causes the issue so that the file doesn't get touched by any minifiers on Github's side: example.jpg.zip The file is a jpg right out of a camera, and while I'm not too familiar with image formats, it seems to me the image data is kept unrotated and then metadata is used to rotate it as it should be? One online exif tool tells me the |
@Etheryte are you saying that the issue reproduces using one of the existing images in |
@benmccann The latter, that the example file I provided reproduces the issue when you put it in the |
I've made a minimal code demo that replicates the issue for me: https://github.com/Etheryte/imagetools-demo. |
This seems to be a bug in Sharp itself, at least for my image, so I've gone ahead and filed an issue there: lovell/sharp#4047 |
@Etheryte's issue on Sharp reveals the way to work around the problem, but it seems like we cannot take advantage of it via the exposed API in imagetools. Summary (hopefully correct): Some images are rotated via metadata, and some are rotated via rearranging the pixels. Sharp drops metadata during resize and so any images rotated via metadata will appear in a surprising orientation after said resize. There is a workaround for this problem, but I think that when we apply rotation via |
Yeah that's a good summary, and as far as Sharp's author is concerned it's working as intended. To fix the problem for Imagetools, the mentioned workaround would have to be integrated here, using the workaround from the currently exposed API does not work as far as I tried it. |
Ah, okay. There is a way for
Seems like there is also potentially a way to use a custom directive to do thing the way you want, but I am not sure if it gives you control over the order of operations, which I think is the sticking point with the It'd be nice if there was a way to whitelist certain metadata and let all the other metadata be stripped. Then we might do
Or something like that. Either way, this does not help in the case of Update: Rather than wait for @benmccann to answer and then wait again for me to open an issue... I went ahead and opened the issue already. Feel free to close it if enhanced:img is not the place to solve this problem. |
Update: Upstreamed into Sharp |
Nice! Let me know if you need help |
I am actually playing with proposing this even further upstream to Sharp. But still playing with an implementation there, as I’ve never worked with C++ before. The PR I have open works, but uses a buffer back into Sharp to set initial orientation in cases where Sharp won’t allow the transformation to work logically (that I could find). If you’d like to use my fork for now, feel free! If the maintainer of Sharp doesn’t like my proposal, I’ll keep tinkering on this to see if I can get it to work without the buffer step in the middle. |
I've finally gotten the code to work, and have opened an issue in Sharp proposing the new feature: lovell/sharp#4144 |
Btw, do you have any idea when this will be merged? Iirc, I can not (easily) use the work around in |
Not soon, but it is still on the map. In the meantime you could potentially employ the same trick I am using in my project to get it to work: apply small patches to both this library and to Sharp. Have a look at my patches directory here: https://github.com/postplayhouse/postplayhouse.com/tree/f524f9549ee7085e5cc6bd0b5a81e2a62347b8b9/patches You’ll want to either use the NPM patch package library, or the built in patching tools provided by pnpm or yarn2. |
This gives an error for me tho:
|
Great news! The next version of Sharp will contain the fulfillment of my proposal to add features to make this whole problem much easier for downstream libraries to solve. This means that imagetools can take advantage of a group of new features, all known as "autoOrient". New Sharp Features (coming in 0.34.0)Instance method
Once you have a sharp instance, you can explicitly call Essentially, it makes Sharp behave the way people think it would if they don't realize their images are oriented with metadata instead of with the actual arrangement of pixels in the file. Option
Same as the above, but you can call it at instantiation instead of after. Both do the same thing under the hood. Composite Option
The same thing, available as a compositing option. Resulting height and width in metadataNew properties are available on image metadata that give you the resulting height and width assuming that you wish to invoke let {
height: originalHeight,
width: originalWidth,
autoOrient: {
height: afterOrientHeight,
width: afterOrientWidth,
}
} = await sharp(image).metadata() RecommendationsI've been digging into this issue rather deeply, starting back in Svelte's I would suggest adding some kind of config option and an explicit url param for consumers. import { defineConfig } from 'vite'
import { imagetools } from 'vite-imagetools'
export default defineConfig({
plugins: [imagetools({
// Runs `.autoOrient()` on all images, unless opted out in the url
defaultAutoOrient: true
// Only runs `.autoOrient()` on images when opted in via the url
defaultAutoOrient: false
})]
}) At the call site: import img1 from "./img1.jpg?autoOrient=true"
import img2 from "./img2.jpg?autoOrient=false" Furthermore, I'd suggest the default behavior for imagetools should be // Next minor release
{ defaultAutoOrient: false }
// Next major release
{ defaultAutoOrient: true } I am guessing it is far less surprising to do the Future workI'd be happy to do the development work on this once the next version of Sharp is released. Honestly, the entire reason I worked on these new features for Sharp were to get them into Assuming this feature is eventually added, I'd suggest that |
I'm using
@sveltejs/enhanced-img
, and for some reason, my optimized images get "flipped" while the original files are the correct side up.+page.server.js
:+page.svelte
This does however not happen with all images for some reason???
Example image which gets rotated:
See below: left image is optimized, right image is original
The text was updated successfully, but these errors were encountered: