Closed Bug 1566316 Opened 6 years ago Closed 5 years ago

HTMLImageElement naturalWidth/naturalHeight does not account for image orientation

Categories

(Core :: DOM: Core & HTML, defect, P3)

defect

Tracking

()

RESOLVED FIXED
mozilla77
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.

Component: DOM: Core & HTML → Layout: Images, Video, and HTML Frames

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.

Flags: needinfo?(geoff)

(I'd argue this is DOM since it's about what gets exposed by the API, there's no layout bug to fix here).

Component: Layout: Images, Video, and HTML Frames → DOM: Core & HTML

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.

Flags: needinfo?(geoff)

Ok so this is just about making these respect the exif metadata, regardless of css, right?

No, because by default we don't rotate the image, so in that case the properties shouldn't change.

(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 as image-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.

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.

Keywords: parity-chrome

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?

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.

It'd be great if you left a comment with your use case in the HTML spec issue, fwiw :)

Priority: -- → P3

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:

  1. Install Firefox for Windows Desktop PC.
  2. Install Zoom Page WE
  3. Configure Zoom Page WE to match this configuration: https://imgur.com/yUNlcHL
  4. 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.

(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:

  1. Install Firefox for Windows Desktop PC.
  2. Install Zoom Page WE
  3. Configure Zoom Page WE to match this configuration: https://imgur.com/yUNlcHL
  4. 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

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"

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.

Taking this to ensure it is resolved before bug 1607667 makes it to beta.

Assignee: nobody → cam
Status: NEW → ASSIGNED
Pushed by cmccormack@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/84726c335808 Make naturalWidth/naturalHeight on images honor orientation metadata. r=emilio
Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/22771 for changes under testing/web-platform/tests
Upstream web-platform-tests status checks passed, PR will merge once commit reaches central.
Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla77
Upstream PR merged by moz-wptsync-bot
Keywords: site-compat
Regressions: 1898170
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: