HTMLImageElement naturalWidth/naturalHeight does not account for image orientation
Categories
(Core :: DOM: Core & HTML, defect, P3)
Tracking
()
Tracking | Status | |
---|---|---|
firefox77 | --- | fixed |
People
(Reporter: darktrojan, Assigned: heycam)
References
(Regressed 1 open bug)
Details
(Keywords: site-compat)
Attachments
(1 file)
For any image that is rotated 90° or 270° by image-orientation: from-image
, the naturalWidth
and naturalHeight
properties should be swapped. Otherwise, calculations based on the image size are incorrect unless the code somehow knows the image has been rotated.
Comment 1•6 years ago
|
||
I think as filed this is WONTFIX. I don't think we want naturalWidth
/ naturalHeight
to flush layout. But I agree there's something to add here...
https://github.com/whatwg/html/issues/4495 contains similar discussion.
Why was the parity-chrome
flag added? Do you have a test-case that works in Chrome? Chrome doesn't support image-orientation
at all, as far as I can tell.
Comment 2•6 years ago
|
||
(I'd argue this is DOM since it's about what gets exposed by the API, there's no layout bug to fix here).
Reporter | ||
Comment 3•6 years ago
|
||
You're right that Chrome doesn't have image-orientation
but it appears to have the same behaviour as image-orientation: from-image
by default.
As for a test case, a photo taken in portrait mode from most digital cameras.
Comment 4•6 years ago
|
||
Ok so this is just about making these respect the exif metadata, regardless of css, right?
Reporter | ||
Comment 5•6 years ago
|
||
No, because by default we don't rotate the image, so in that case the properties shouldn't change.
Comment 6•6 years ago
|
||
(In reply to Geoff Lankow (:darktrojan) from comment #3)
You're right that Chrome doesn't have
image-orientation
but it appears to have the same behaviour asimage-orientation: from-image
by default.
This doesn't match my testing?
-
data:text/html,<img src="https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Portrait_5.jpg">
renders the same in Chrome and Firefox. -
data:text/html,<img src="https://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Portrait_5.jpg" style="image-orientation: from-image">
renders correctly in Firefox, but not Chrome.
Reporter | ||
Comment 7•6 years ago
|
||
Huh. I'd only been looking at the stand-alone image in Chrome, not in an img tag. They must do something fancy with their stand-alone viewer, because it's just an HTML document with an img tag in it AFAICT. I guess this isn't really a parity issue, which isn't why I wanted it fixed anyway.
Comment 8•6 years ago
|
||
Geoff, would an API like naturalOrientation
(or something of that sort) fix your use-case? You can query the computed value of the image-orientation
property anyway, so you could do something like:
function adjustedImageDimensions(img) {
let w = img.naturalWidth;
let h = img.naturalHeight;
if (getComputedStyle(img).imageOrientation == "from-image") {
// Update w and h, based on the return value of naturalOrientation. Or something like that.
}
return { w, h };
}
The reasons for this are various, but mostly:
- Avoid depending on layout (since it can affect perf).
- Define them properly when e.g., the element is not in the DOM. Otherwise there's no good way to figure out the natural orientation of an image outside of the DOM, which is possible via the the
decode()
API.
Does that make sense? Alternatively maybe a orientedNaturalWidth
/ orientedNaturalHeight
thing?
Reporter | ||
Comment 9•6 years ago
|
||
Yes that would be useful, because even in your example getComputedStyle(img).imageOrientation == "from-image"
doesn't tell you if an image was rotated, just that it might have been rotated if the EXIF data called for it.
naturalOrientation
could be portrait
/landscape
, or 0
/90
/180
/270
or something like it. IIRC there's 8 possible values in EXIF but I doubt it's necessary to have as much information as that provides.
For both the uses I have in mind, orientedNaturalWidth
/orientedNaturalHeight
would be sufficient and save an if…else block.
Comment 10•6 years ago
|
||
It'd be great if you left a comment with your use case in the HTML spec issue, fwiw :)
Updated•6 years ago
|
Comment 11•6 years ago
|
||
I've experienced this issue with several releases of Firefox, specifically images hosted on Discord trigger the problem when they are manipulated by Zoom Page WE add-on.
To reproduce:
- Install Firefox for Windows Desktop PC.
- Install Zoom Page WE
- Configure Zoom Page WE to match this configuration: https://imgur.com/yUNlcHL
- Open an image hosted on discord, such as: https://cdn.discordapp.com/attachments/129399825342005250/612401472025657344/image0.jpg or alternatively https://cdn.discordapp.com/attachments/129399825342005250/574403266293792809/20190504_195328.jpg
The image aspect ratio will be inverted.
Comment 12•6 years ago
|
||
(In reply to Franpa_999 from comment #11)
I've experienced this issue with several releases of Firefox, specifically images hosted on Discord trigger the problem when they are manipulated by Zoom Page WE add-on.
To reproduce:
- Install Firefox for Windows Desktop PC.
- Install Zoom Page WE
- Configure Zoom Page WE to match this configuration: https://imgur.com/yUNlcHL
- Open an image hosted on discord, such as: https://cdn.discordapp.com/attachments/129399825342005250/612401472025657344/image0.jpg or alternatively https://cdn.discordapp.com/attachments/129399825342005250/574403266293792809/20190504_195328.jpg
The image aspect ratio will be inverted.
Example of what firefox displays under this condition: https://imgur.com/5P6co9a
Comment 13•6 years ago
|
||
Well, it seems to me that that add-on is relying on a blink hack, which explicitly says: "If this is an image document, then use the exif data, otherwise don't"
Assignee | ||
Comment 14•5 years ago
|
||
With image-orientation: from-image
soon to be the default, I tend to agree that naturalWidth
and naturalHeight
should honor the EXIF orientation of the image.
Assignee | ||
Comment 15•5 years ago
|
||
Taking this to ensure it is resolved before bug 1607667 makes it to beta.
Assignee | ||
Updated•5 years ago
|
Assignee | ||
Comment 16•5 years ago
|
||
Assignee | ||
Comment 17•5 years ago
|
||
Comment 18•5 years ago
|
||
Comment 21•5 years ago
|
||
bugherder |
Updated•5 years ago
|
Description
•