-
Notifications
You must be signed in to change notification settings - Fork 138
Fix Modern Image Formats picture element support for selected image sizes #1328
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
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @rajkpatel@1728@gmail.com, @imrraaj. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
This PR should also include some test cases that demonstrate the original bug. Perhaps start there before adding the required fix, for some good ol' test-driven development. |
|
Based on my observations, the issue only affects JPG and JPEG images. Images with different MIME types are working as expected. The issue arises when the user has a JPG or JPEG image and the image size is set to |
| // Ensure that the source element exists in the picture element and has the correct mime type. | ||
| $webp_source = '<source type="image/webp"'; | ||
| $jpg_source = '<source type="image/jpeg"'; | ||
| $this->assertStringContainsString( $webp_source, $the_image ); | ||
| $this->assertStringContainsString( $jpg_source, $the_image ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rather do a single assertSame assertion? By checking the entire string we can be extra confident it contains everything we expect.
|
I verified the change worked as expected. I'm curious why thumbnail sizes in particular are handled this way. I checked with default WordPress without the plugin and noticed that no If I select the medium size, I get a full srcset: This explains why the bug exists; for the picture element support we want to always have these srcsets so we can provide a fallback. I'm going to dig a little more to see if I can tell why core doesn't use |
In looking at Since thumbnails are square, as I understand they would only get a populated I just tried uploading a square image and then inserting it with the thumbnail resolution, and the generated markup was: <img
decoding="async"
width="150"
height="150"
src="http://localhost:8888/wp-content/uploads/2024/07/pexels-mark-a-jenkins-373676419-21771035-150x150.jpg"
alt=""
class="not-transparent wp-image-43"
srcset="http://localhost:8888/wp-content/uploads/2024/07/pexels-mark-a-jenkins-373676419-21771035-150x150.jpg 150w, http://localhost:8888/wp-content/uploads/2024/07/pexels-mark-a-jenkins-373676419-21771035-300x300.jpg 300w, http://localhost:8888/wp-content/uploads/2024/07/pexels-mark-a-jenkins-373676419-21771035-1024x1024.jpg 1024w, http://localhost:8888/wp-content/uploads/2024/07/pexels-mark-a-jenkins-373676419-21771035-768x768.jpg 768w, http://localhost:8888/wp-content/uploads/2024/07/pexels-mark-a-jenkins-373676419-21771035-1536x1536.jpg 1536w, http://localhost:8888/wp-content/uploads/2024/07/pexels-mark-a-jenkins-373676419-21771035-2048x2048.jpg 2048w, http://localhost:8888/wp-content/uploads/2024/07/pexels-mark-a-jenkins-373676419-21771035-1568x1568.jpg 1568w"
sizes="(max-width: 150px) 100vw, 150px">So here, even though the thumbnail was inserted, it did get a fully-populated |
this makes me wonder if the fix proposed here is correct. If a non-square image is uploaded, i suspect the fallbacks specified in the srcset entries will be distorted when displayed. Since no srcset is provided, we could instead use the maybe we could construct srcsets with the original image urls in each format (untested): so for example would become note the |
adamsilverstein
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs some refinement to use only the cropped images (with matching sizes) - see comment thread
|
Punting this to the next release. |
|
So I tried uploading a non-square image to test the code in the PR. When the thumbnail size is selected, it generates following HTML. <picture class="wp-picture-267" style="display: contents;">
<source type="image/webp" srcset="http://contrib.local/wp-content/uploads/2024/07/lake-8847415_1280-150x150.jpg" sizes="(max-width: 150px) 100vw, 150px">
<source type="image/jpeg" srcset="http://contrib.local/wp-content/uploads/2024/07/lake-8847415_1280-150x150.jpg" sizes="(max-width: 150px) 100vw, 150px">
<img width="150" height="150" src="http://contrib.local/wp-content/uploads/2024/07/lake-8847415_1280.jpg" class="attachment-150x150 size-150x150" alt="">
</picture>So as per @adamsilverstein's suggestion along with source urls, the |
|
@westonruter @adamsilverstein This has been solved in #1354 and merged in trunk. The IMG src check is added in #1408. Closing this PR now. |


Description
This PR addresses an issue with the Modern Image Formats plugin's picture element support, where the generated markup did not account for the image sizes selected from the editor.
Fixes #1316
Problem
When using the Modern Image Formats plugin with picture element support enabled, the generated markup would load the full-size image instead of the selected image size (e.g., Thumbnail) and the elements were missing for the selected image size.
Solution
To fix this issue, the PR introduces the following changes:
$sizesis false or$image_srcsetis empty, then$image_srcsetshould be set to the modern image format version of the$src.$image_srcsetthen correctly generates the source tags and correct image size is set based on the value selected in the editorTesting
All the test are passing locally.
Outcome