Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Post Featured Image]: Add image size controls #33789

Closed
ntsekouras opened this issue Jul 31, 2021 · 43 comments · Fixed by #38044
Closed

[Post Featured Image]: Add image size controls #33789

ntsekouras opened this issue Jul 31, 2021 · 43 comments · Fixed by #38044
Assignees
Labels
[Block] Post Featured Image Affects the Post Featured Image Block [Package] Block library /packages/block-library [Type] Enhancement A suggestion for improvement.

Comments

@ntsekouras
Copy link
Contributor

What problem does this address?

We should add image size controls to Post Featured Image block (e.g. full, thumbnail, medium, large, custom-theme-size, etc..). Here is @justintadlock 's comment for reference.

To make sure feedback isn't lost from this recent WP Tavern post, I'm sharing here as it's relevant to the discussion above:

There is no way to set the size of the image output by the Post Featured Image block. The only way to get a uniform size at the moment is to pre-crop the images before uploading them to WordPress. There is no reason this should not essentially be a variation of the Image block. The only thing featured images need that is different is the option to link to the post.

This was identified as a current pain point in the FSE experience.

Initial design from @jasmussen:

Featured Image i3

@ntsekouras ntsekouras added [Type] Enhancement A suggestion for improvement. [Package] Block library /packages/block-library [Block] Post Featured Image Affects the Post Featured Image Block labels Jul 31, 2021
@Mamaduka
Copy link
Member

Mamaduka commented Aug 2, 2021

This looks like a good enhancement for the ImageSizeControl component.

@markhowellsmead
Copy link

An additional note on this: the current function (which uses get_the_post_thumbnail) currently outputs the thumbnail HTML for the full-sized image. This means that in a basic install where the registered image size thumbnail is set to cropped 400x400, this small thumbnail isn't used.

@ibes
Copy link

ibes commented Nov 5, 2021

@markhowellsmead
I don't get why the full-size image is pulled after all.
Can you support me understand that? 'post-thumbnail' is default so I don't get where 'full' is introduced.

I try to figure out a way how to use existing filters to make that work.
All I found so far is https://generatepress.com/forums/topic/query-loop-post-template-image-size/
Which has no effect on the image that is actually loaded - just effects the image display dimensions.

@markhowellsmead
Copy link

There appears to be a little confusion in the code, in as far as there being two thumbnail sizes in the code: thumbnail and post-thumbnail. I've run through the function stack and debugged this process and it doesn't look as though this is the problem.

The function wp_get_attachment_thumb_file looks for the thumbnail for the image. I've regenerated the thumbnails for my images and they are correctly stored, and the attachment meta data is created. This function searches for an array entry $imagedata['thumb'].

However, as far as I can see, the image regeneration function wp_create_image_subsizes doesn't add an entry to the meta data array with the key thumb, so this will never be found. Therefore, the full-size image is selected.

@markhowellsmead
Copy link

I've added this to Trac at https://core.trac.wordpress.org/ticket/54383.

@markhowellsmead
Copy link

The response in Trac is that the issue was first flagged in https://core.trac.wordpress.org/ticket/17262.

Eleven years ago.

You might want to find a different solution.

@ibes
Copy link

ibes commented Nov 10, 2021

Oh wow - this is absurd.
Useless gutenberg blocks because a core function was not fixed in 11 years ...

Thanks for going down the rabbit hole @markhowellsmead

@markhowellsmead Would you be open to climb down the rabbit hole together to figure out where to put some tape?
Maybe there is a way to deliver wp_get_attachment_thumb_file with the $imagedata['thumbnail] and than hook into that process to end up with a more healthy fallback than full?!

I used the latest posts blocks for blog posts. But am back with wanting to query CPTs.
And I would like to stay as good with core blocks as possible. Not pulling into a whole group of oppinionated blocks just to compensate for this strange behaviour.

@ibes
Copy link

ibes commented Nov 11, 2021

add_filter( 'post_thumbnail_size', 'update_default_thumbnail_size_for_function_get_the_post_thumbnail' );
function update_default_thumbnail_size_for_function_get_the_post_thumbnail( $size ) {
	if ( $size === 'post-thumbnail' ) {
		return 'medium';
	}
	return $size;
}

this might help to set an default image size smaller than full

It replaces the unset post-thumbnail with medium.

Seems to work for me in the frontend, but not in the editor/backend.
Don't get why.

@markhowellsmead
Copy link

I don't think it's a brilliant idea to remove the opportunity to use the post thumbnail in this way. (It might make sense for an individual developer to use this, if that's a project requirement.)

Replacing post-thumbnail with thumbnail using this filter works, returning the correctly-sized thumbnail image for the post-featured-image block.

add_filter( 'post_thumbnail_size', 'update_default_thumbnail_size_for_function_get_the_post_thumbnail' );
function update_default_thumbnail_size_for_function_get_the_post_thumbnail( $size ) {
	if ( $size === 'post-thumbnail' ) {
		return 'thumbnail';
	}
	return $size;
}

@markhowellsmead
Copy link

For the post-featured-image block, I suggest modifying the call in the render_block_core_post_featured_image method from

$featured_image = get_the_post_thumbnail( $post_ID );

to

$featured_image = get_the_post_thumbnail( $post_ID, 'thumbnail' );

@dreamwhisper
Copy link

I'd like to see the full, thumbnail, medium, large, & custom-theme-size(s) selectable to allow for greater flexibility in theme design, especially when the same image may be shown at different sizes throughout a site.

@hanopcan
Copy link

hanopcan commented Jan 5, 2022

I'm also keen to see this feature hit production as from a performance point of view not being able to control the size of the featured image is a problem 💚

@Mamaduka
Copy link
Member

I am assigning this to myself. I will have PR ready shortly.

@Mamaduka Mamaduka self-assigned this Jan 13, 2022
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Jan 18, 2022
@apeatling apeatling moved this from ⏱ Not Started to ⏳ In Progress in Design Tools Project Jan 18, 2022
Repository owner moved this from ⏳ In Progress to ✅ Done in Design Tools Project Jan 20, 2022
@errotu
Copy link

errotu commented Nov 13, 2022

It's rather hidden: You have to click on the three dots next to "Dimensions" ("Größe") and select "Image Size".

@markhowellsmead
Copy link

@errotu The controls for the Post Featured Image block in the Single template are completely different than in the Archive view. Hardly any of those controls are available and there's no option to choose the image size from the list of preset sizes.

@EricWorrall
Copy link

You can use editor.PostFeaturedImage to append checkboxes and other controls to the featured image block on the post editor. The options can then be used to modify the display of the featured image. I did this with Magazine Plus, which uses some non-standard hooks for displaying posts, but you can also do it with more standard hooks. If all else fails you can use the options to control whether CSS scripts are enqueued which create the outcome you want.

This post got me pointed in the right direction.

@justintadlock
Copy link
Contributor

You should see an "Image Size" option if you click the "vertical ellipsis" button under the "Dimensions" panel. If you check that option, a new "Image Size" dropdown select should appear:

image

@nicmare
Copy link

nicmare commented Apr 4, 2023

You should see an "Image Size" option if you click the "vertical ellipsis" button under the "Dimensions" panel. If you check that option, a new "Image Size" dropdown select should appear:

image

wow unbelievable i had to google this and making at least 3 clicks for this hidden feature. thank you !

@scrobbleme
Copy link

This should be opened again. There is still no option to choose a predefined size for single templates.

@vayurobins
Copy link

It's still available @scrobbleme ;-), it's just called "Resolution" now.
Screenshot 2023-09-03 at 20 37 49

@scrobbleme
Copy link

scrobbleme commented Sep 4, 2023

It's still available @scrobbleme ;-), it's just called "Resolution" now.

@vayurobins Yes, but not when editing templates within the site editor. The is no resolution available.

@vayurobins
Copy link

@scrobbleme, that screenshot is from the Full Site Editor, and I was editing the Archives Template. Using Twenty Twenty Three theme.

@scrobbleme
Copy link

@scrobbleme, that screenshot is from the Full Site Editor, and I was editing the Archives Template. Using Twenty Twenty Three theme.

@vayurobins Sorry, it is not working for single templates. "Loops" are fine. I just saw, that there is a new issue for this: #50271

@markhowellsmead
Copy link

If I choose the thumbnail size, then the following HTML is generated.

<img width="200" height="150" src="https://WEBSITE/wp-content/uploads/2016/05/20160521_DSCF6661-200x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" decoding="async" style="object-fit:cover;">

If I choose the "medium" size, then the following HTML is generated. My medium size is 720px, but the srcset options go up to 1600px. Is this intended behaviour? 🤔

<img width="720" height="480" src="https://WEBSITE/wp-content/uploads/2016/05/20160521_DSCF6661-720x480.jpg" class="attachment-medium size-medium wp-post-image" alt="" decoding="async" style="object-fit:cover;" fetchpriority="high" srcset="https://WEBSITE/wp-content/uploads/2016/05/20160521_DSCF6661-720x480.jpg 720w, https://WEBSITE/wp-content/uploads/2016/05/20160521_DSCF6661-1600x1067.jpg 1600w, https://WEBSITE/wp-content/uploads/2016/05/20160521_DSCF6661-1024x683.jpg 1024w" sizes="(max-width: 720px) 100vw, 720px">

@Mamaduka Mamaduka removed the [Status] In Progress Tracking issues with work in progress label Sep 4, 2023
@nicmare
Copy link

nicmare commented Sep 4, 2023

If I choose the thumbnail size, then the following HTML is generated.

<img width="200" height="150" src="https://WEBSITE/wp-content/uploads/2016/05/20160521_DSCF6661-200x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" decoding="async" style="object-fit:cover;">

If I choose the "medium" size, then the following HTML is generated. My medium size is 720px, but the srcset options go up to 1600px. Is this intended behaviour? 🤔

<img width="720" height="480" src="https://WEBSITE/wp-content/uploads/2016/05/20160521_DSCF6661-720x480.jpg" class="attachment-medium size-medium wp-post-image" alt="" decoding="async" style="object-fit:cover;" fetchpriority="high" srcset="https://WEBSITE/wp-content/uploads/2016/05/20160521_DSCF6661-720x480.jpg 720w, https://WEBSITE/wp-content/uploads/2016/05/20160521_DSCF6661-1600x1067.jpg 1600w, https://WEBSITE/wp-content/uploads/2016/05/20160521_DSCF6661-1024x683.jpg 1024w" sizes="(max-width: 720px) 100vw, 720px">

yes sure what else did you expect?

@markhowellsmead
Copy link

Errr… that the image displayed in the frontend would be the "medium" image size. (It isn't: it's always the "large" image size. And yes, I'm fully aware of how the srcset works on retina screens.)

@nicmare
Copy link

nicmare commented Sep 4, 2023

if the viewport is large enough its loading the high resolution image from the srcset. by default your medium image (20160521_DSCF6661-720x480.jpg)

@markhowellsmead
Copy link

markhowellsmead commented Sep 4, 2023

Nope, it always loads 20160521_DSCF6661-1600x1067.jpg, irrespective of screen size on screens at or larger than 720px.

@nicmare
Copy link

nicmare commented Sep 4, 2023

if you expect something different or have more in depth questions to that specific behavior, you should better create a new thread. this one here is about the image size / resolution control in gutenberg.

@markhowellsmead
Copy link

Forgive me for misunderstanding: how does adding the block in the single template fix the issue in the post loop?

@nicmare
Copy link

nicmare commented Sep 4, 2023

@scrobbleme, that screenshot is from the Full Site Editor, and I was editing the Archives Template. Using Twenty Twenty Three theme.

@vayurobins Sorry, it is not working for single templates. "Loops" are fine. I just saw, that there is a new issue for this: #50271

thats true. noticed this as well. but as a work around simply copy the featured image block from loop with "resolution" to medium or what ever set and paste it in your single page template. you will notice it now loads the right image size

@scrobbleme
Copy link

Forgive me for misunderstanding: how does adding the block in the single template fix the issue in the post loop?

@markhowellsmead It doesn't ;)
"Loop" (like archive or just query loop block) and "Single" are different… loop is working as expected ;)

But there are requirements to show a custom resolution ("add_image_size") in a single template as well (i.e. migrating legacy sites to the block editor)

@Luke-SF
Copy link

Luke-SF commented Sep 26, 2023

@vayurobins Yes, but not when editing templates within the site editor. The is no resolution available.

Not seeing the resolution picker on normal Image block. Oddly enough resolution does show in the [ settings ... ] line drop down menu on the block editor.

@sinanisler
Copy link

custom sizes are not visible.
image

add_image_size( 'size-400', 400, 400, true );

@vollyimnetz
Copy link

vollyimnetz commented Dec 18, 2023

If you are programming your own block-theme you may use the attribute "sizeSlug" to set the custom-image-size "by hand".

<!-- wp:post-featured-image {"sizeSlug":"artikelbild","overlayColor":"contrast","dimRatio":0,"style":{"spacing":{"margin":{"bottom":"var:preset|spacing|50","top":"calc(-1 * var(--wp--preset--spacing--50))"}}}} /-->

Found in: https://github.com/WordPress/gutenberg/blob/trunk/packages/block-library/src/post-featured-image/block.json

@SalvatoreNoschese
Copy link

I'm here exactly for same reason.
I use bitàcora and updated WordPress+litespeed server/plugin.
Cannot found any way to set small or medium featured image size and this cause a poor lcp value.
Screenshot_20240524-154304

@justintadlock
Copy link
Contributor

Folks, if y'all are running into other issues, please open new tickets. The specific issue was addressed.

@sinanisler - Custom image sizes do not automatically appear. You have to add those you want to the dropdown via: https://developer.wordpress.org/reference/hooks/image_size_names_choose/

@markhowellsmead
Copy link

markhowellsmead commented Aug 7, 2024

The image resolution control for the Post Featured Image Block is still not available on the Single Posts (single.html) view in the Site Editor. Running 6.6.1.

@ajgagnon
Copy link

Yes, why was this skipped for featured images?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Featured Image Affects the Post Featured Image Block [Package] Block library /packages/block-library [Type] Enhancement A suggestion for improvement.
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.