Skip to content

Avoid amp-img srcset selecting a smaller image if it will look unacceptably distorted#8055

Merged
dvoytenko merged 1 commit into
ampproject:masterfrom
brandcast:srcset-diff-ratio
Mar 13, 2017
Merged

Avoid amp-img srcset selecting a smaller image if it will look unacceptably distorted#8055
dvoytenko merged 1 commit into
ampproject:masterfrom
brandcast:srcset-diff-ratio

Conversation

@acusti
Copy link
Copy Markdown
Contributor

@acusti acusti commented Mar 10, 2017

Account for proportionality ratio to srcset select calculation when choosing a smaller image over a larger one

Currently the srcset selection chooses the closest size, favoring the larger size by 10%. So if you have a 400px wide and 800px wide version of an image in the srcset, and the current <amp-img> element width is 590px (590 - 400 = 190 * 1.1 = 209; 800 - 590 = 210), amp selects the 400px version and you wind up getting a highly distorted image rendering. However, if you had 1400px and 1800px wide versions of an image, and the current element width is 1590px, choosing the 1400px version is a good call. The selection algorithm needs to account for proportionality to predict how distorted the image will wind up looking.

In this PR, I chose to make 1.2 the maximum acceptable ratio of element width to image width for choosing a smaller image. In specific terms, my first example: 590 / 400 = 1.475, which would be above the max ratio, so srcset selects the larger image. In the 2nd example: 1590 / 1400 = 1.136, ratio is below 1.2, so <amp-img> goes with the smaller image.

This also scales down well to smaller image sizes. I recently had an instance where we had a 30px and 120px wide image version in the srcset. The element was being rendered at 70px, and <amp-img> was choosing the 30px version, which of course looks unacceptable. The ratio makes it obvious: 70 / 30 = 2.33.

Related-to #1280

@googlebot
Copy link
Copy Markdown

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If you signed the CLA as a corporation, please let us know the company's name.

@acusti acusti mentioned this pull request Mar 10, 2017
@jridgewell
Copy link
Copy Markdown
Contributor

LGTM, but deferring to @dvoytenko.

@acusti
Copy link
Copy Markdown
Contributor Author

acusti commented Mar 11, 2017

Note that I have now signed the CLA

@googlebot
Copy link
Copy Markdown

CLAs look good, thanks!

Copy link
Copy Markdown
Contributor

@dvoytenko dvoytenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! Looks great!

@dvoytenko dvoytenko merged commit 82f0c0a into ampproject:master Mar 13, 2017
mrjoro pushed a commit to mrjoro/amphtml that referenced this pull request Apr 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants