jetpack icon indicating copy to clipboard operation
jetpack copied to clipboard

Display WordPress Posts Widget: Resize Featured Images

Open RCowles opened this issue 11 years ago • 13 comments

The Featured Images in the Display WordPress Posts widget are pulled in at full size and scaled down using CSS. Depending upon the source, these Featured Images can be quite large. It would be nice if we could serve scaled down images instead.

RCowles avatar May 20 '15 14:05 RCowles

Reclassifying as a bug. We shouldn't ever have been pulling down a 5000x5000 featured image to fit in a little box.

kraftbj avatar May 26 '15 15:05 kraftbj

Suggested here: https://wordpress.org/support/topic/scaled-images-in-display-wordpress-posts-widget?replies=8

RCowles avatar May 27 '15 22:05 RCowles

Relevant code:

https://github.com/Automattic/jetpack/blob/bc3c37ee033a4b516d59eeacca6f5fdd64b0c19b/modules/widgets/wordpress-post-widget.php#L120-L123

georgestephanis avatar May 28 '15 15:05 georgestephanis

Just pass it through jetpack_photon_url with a max size in that while loop.

georgestephanis avatar May 28 '15 15:05 georgestephanis

Just pass it through jetpack_photon_url with a max size

I had forgotten about that issue and implemented something related in #2193.

I didn't specify any max size, though, because I don't know what that size should be. I can't think of a way to guess how wide a site's sidebar can be. We'd need a $sidebar_width theme feature! :) reference

jeherve avatar Jun 01 '15 13:06 jeherve

I know that maybe this wouldn't be a perfect solution, but couldn't we approach this problem the other way around? I.e. setting a reasonable max width and letting people customize it if it's too small?

I know that sidebars aren't anymore the simple columns on the side of a blog, but a layout element that could be used in every part of a theme. But I think that the average theme sidebar width is between 300px and 600px, and widget areas larger than that aren't very common (maybe I could gather some data on that).

A 600px wide image doesn't look too bad if it's stretched at 1000px, and it could save a lot of bandwidth.

I've done some testing fetching 10 recent posts from en.blog.wordpress.com and displaying them in an empty page template (the worst case, a full-width "sidebar")

This is the current situation

Full size images

The posts are fetched with a full-size image, totaling 2.3MB.

This is the case passing a default array( 'w' => 600 ) to the jetpack_display_posts_widget_image_params

600 px images

Total size is 250 KB

And this is the case with images 600px wide, stretched in a 1000px container

image

The images aren't very sharp, but acceptable, in my opinion. This would be the case where the filter defaults would be overridden

jubstuff avatar Apr 19 '16 21:04 jubstuff

I.e. setting a reasonable max width and letting people customize it if it's too small?

I think a default max size is reasonable, though we shouldn't be upscaling images. Also, we should avoid another option (especially a theme dependent one) if at all possible.

If I recall correctly, our Photon module includes some JS that can check and see if a photon image is scaled down too small for the layout on the page and changes the src attribute to a URL for an appropriately sized image. Or this may be an opportunity to use srcset as core does in places (which may be what the Photon JS I mentioned is doing now; I'm not sure).

Could you take a look at that Photon JS code? I'm not sure if I'm remembering correctly or what the pros/cons might be :) I think @kraftbj has looked into this a bit recently too, so he may have some insight.

mdawaffe avatar Apr 27 '16 08:04 mdawaffe

Thank you @mdawaffe

I've read through the photon.js file, but it seems that it wouldn't help here. It adds the width and height attributes to the img elements with data-recalc-dims set.

Those img elements will then be caught by devicepx-jetpack.js to be rescaled to load retina/HiDPI versions of files (if I have reconstructed the flow correctly), but the image is already downloaded at that time.

Regarding srcset and sizes: I'm by no means a front-end expert, but I think that in this case they wouldn't be much of use either.

We could add several breakpoints like @kraftbj pointed out in this comment, but the main difference here is that we are not dealing with $content_width.

The various widths specified would refer to the total viewport size and to have them work we'd need something like $sidebar_width, but it wouldn't make sense as @jeherve note in that ticket.

I read something about Element queries that are like media queries that apply to the width/height of the element's container. But it seems that they're not production-ready.

I can't think of a way other than setting a reasonable default width and customize it using a filter, right now. I will dig further, though ;)

jubstuff avatar Apr 28 '16 14:04 jubstuff

Those img elements will then be caught by devicepx-jetpack.js to be rescaled to load retina/HiDPI versions of files (if I have reconstructed the flow correctly), but the image is already downloaded at that time.

What if the initial image was small (or a best guess), and then we dynamically loaded a larger image if we needed to to avoid upscaling?

Regarding srcset and sizes: I'm by no means a front-end expert, but I think that in this case they wouldn't be much of use either.

My understanding is probably more naïve than yours, but it seems like a good solution to me. Why doesn't it work?

mdawaffe avatar May 14 '16 00:05 mdawaffe

What if the initial image was small (or a best guess), and then we dynamically loaded a larger image if we needed to avoid upscaling?

Honestly, I don't like this approach, as it will result in two images being downloaded, as it already happens: if you examine this HAR file taken from my Android phone you can notice that the img_20160401_095612.jpg image (which is 1MB) is loaded twice.

My understanding is probably more naïve than yours, but it seems like a good solution to me. Why doesn't it work?

It doesn't work because we can't know the widget area's width in advance. In this PR, @kraftbj used $content_width and meta information to build the srcset array. But we don't have this information with images coming from external sources.

Looking at @kraftbj's code, however, gave me an idea: we could set $content_width as a max size for the images. It should be reasonably safe as usually sidebars and widget areas are narrower than the main content area. This could be filterable through jetpack_display_posts_widget_image_params and could potentially save some KBs while looking for a more robust solution

jubstuff avatar May 21 '16 12:05 jubstuff

This issue has been marked as stale. This happened because:

  • It has been inactive in the past 6 months.
  • It hasn’t been labeled `[Pri] Blocker`, `[Pri] High`.

No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.

stale[bot] avatar Sep 19 '18 15:09 stale[bot]

This issue has been marked as stale. This happened because:

  • It has been inactive in the past 6 months.
  • It hasn’t been labeled `[Pri] Blocker`, `[Pri] High`, etc.

No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.

github-actions[bot] avatar Jul 27 '21 00:07 github-actions[bot]

Since WordPress Core now comes with srcset values, and one can use blocks to display posts instead of the widget, I believe this issue is not as important as it once was. I'll consequently close it since it hasn't seen much activity in the past few years. We can of course always reopen later if necessary.

jeherve avatar Dec 17 '24 18:12 jeherve