jetpack icon indicating copy to clipboard operation
jetpack copied to clipboard

Photon: HiDPI + srcset

Open kraftbj opened this issue 11 years ago • 10 comments

WordPress 4.4 adds native srcset support. Core currently only look to the width descriptor so browsers can choose the right image width based on need. The spec also supports pixel density, e.g. 1x, 1.5x, 2x, etc, so we should be able to use this, possibly deprecating device-px.js (within Jetpack at least) within a few versions.

Initial read is that you can't use both the w and x descriptors for the same image set, though.

Marking as 3.9 for now.

kraftbj avatar Oct 29 '15 03:10 kraftbj

I'm not a developer, but I'm fascinated with responsive images and have been reading up on them a lot lately. It's my understanding that srcset supports pixel density even with just the w descriptor.

For example, if sizes calls for a 300px wide image to be used, but the user is on a 2x display, it will grab a 600px wide image instead.

ChrisBegley avatar Oct 29 '15 04:10 ChrisBegley

Let's take a look at it in 4.0

zinigor avatar Jan 19 '16 08:01 zinigor

    public function filter_sizes_array( $sizes ){
        global $content_width;
        $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $content_width );
        return $sizes;
    }

adding that restricts it to the $content_width preventing a full-size image from loading (maxing at the $content_width, but $content_width isn't universally used well-enough to rely on it.

If we can grab the width arg from the html attribute, if present, would that be reasonable to set it as the max-width of the sizes array?

kraftbj avatar Apr 27 '16 05:04 kraftbj

After typing up a brain dump on a p58i-3xf-p2 , another approach is making more sense to me. We can expand the srcset array to include percentages of the full-size image. For example, with a 5000px full-size image with native WP sizes of 1024px, 600px, 150px, we add in 80%, 60%, 40%, 20% of the full-size resulting in options of 5000, 4000, 3000, 2000, 1024, 1000, 600, 150.

When the needed width is 1100, this would result in the 2000px image being selected by the browser instead of the 5000px image. It's still solidly larger than what is needed but quite a bit less of a variance than the current situation, which would be using the full-size image.

kraftbj avatar Apr 27 '16 05:04 kraftbj

Love the % idea — but I think we should first consider a $content_width implementation as the gold standard and best UX.

It's the standard because it's required for themes in the WP.org and WP.com directories and has been in use in default themes since Twenty Ten.

It's the best UX because it ensures an image isn't loaded that's any wider than the content width for desktop sized browsers.

$content_width isn't universally used well-enough to rely on it.

I don't think this means we shouldn't use it! For themes that don't follow that WordPress theming standard, other techniques can be used as fallbacks.

lancewillett avatar Apr 27 '16 14:04 lancewillett

@lancewillett We'd need to provide some variations of $content_width ( 2x, 4x, etc ) as well. I wrote #3748 on the plane before seeing your comment, so I didn't do anything with $content_width.

The $content_width isn't as helpful if the image isn't being added at the full content_width size, e.g. if the image is being loaded at, say 700x500, while the original is 2800x2000 and the content width is 850, then we'll have mismatch too.

I think any approach we do will have some extra pixels somewhere. I'm hesitate to put in too much since this is a "display" filter being run on every image on each page load (caching notwithstanding). We can continue talking implementation in #3748 now there's some code to poke apart.

kraftbj avatar Apr 29 '16 16:04 kraftbj

I think one of the main pain points we're trying to solve here is to stop loading a HUGE image on small screens. So the 2x and 4x variants don't depend as much on content_width and could instead just be based on something else (cropped version of original).

But the mobile and small screen image should be maxed out always at content_width. Does that make sense? That would be a simple and quick fix for the most egregious issue where a visitor uses high bandwidth and high loading times on mobile devices.

lancewillett avatar Apr 29 '16 16:04 lancewillett

I'm not arguing that we can't serve really perfectly sized images for desktop browsers. :) But it seems like a nice-to-have feature — whereas mobile images loading at huge sizes is a bad bug.

lancewillett avatar Apr 29 '16 16:04 lancewillett

OK: I understand better now — even if the image is the size given by using content_width as the width in pixels, that might be too small for the pixel density in the srcset — which means the browser would still go grab the full-size image on a retina mobile device.

lancewillett avatar Apr 29 '16 16:04 lancewillett

Fixed in #3748. devicepx-jetpack.js may need some love, but will open an atomic issue(s) to address specific things there.

kraftbj avatar Jun 17 '16 16:06 kraftbj