Skip to content

Conversation

@imrraaj
Copy link

@imrraaj imrraaj commented Jul 2, 2024

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:

  • When the $sizes is false or $image_srcset is empty, then $image_srcset should be set to the modern image format version of the $src.
  • $image_srcset then correctly generates the source tags and correct image size is set based on the value selected in the editor

Testing

All the test are passing locally.

Outcome

Screenshot 2024-07-02 at 6 15 07 PM

@github-actions
Copy link

github-actions bot commented Jul 2, 2024

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 props-bot label.

Unlinked Accounts

The 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.

Unlinked contributors: rajkpatel@1728@gmail.com, imrraaj.

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: mukeshpanchal27 <mukesh27@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@imrraaj imrraaj marked this pull request as draft July 2, 2024 13:00
@westonruter westonruter added the [Type] Bug An existing feature is broken label Jul 2, 2024
@westonruter westonruter added this to the webp-uploads n.e.x.t milestone Jul 2, 2024
@westonruter westonruter added the [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) label Jul 2, 2024
@westonruter
Copy link
Member

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.

@imrraaj
Copy link
Author

imrraaj commented Jul 4, 2024

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 thumbnail. When we select the thumbnail image size, it does not output the source tags which contain information about the srcset attribute, ultimately rendering the entire image as is with the given height and width. All the image sizes apart from thumbnail work fine (i.e., medium, large, full size).
I have written a test case just to demonstrate this. It will fail on the current state of the main branch, requiring the fix to be applied.

Comment on lines +115 to +119
// 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 );
Copy link
Member

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.

@adamsilverstein
Copy link
Member

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 srcset is output when I select the thumbnail size, only the image tag:
image

If I select the medium size, I get a full srcset:
image

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 srcset for thumbnails (or @joemcgill may know?) - probably because the images are small already so adding additional sizes doesn't make sense?

@westonruter
Copy link
Member

I'm going to dig a little more to see if I can tell why core doesn't use srcset for thumbnails - probably because the images are small already so adding additional sizes doesn't make sense?

In looking at wp_calculate_image_srcset() I see it has these comments:

 	 * Loop through available images. Only use images that are resized
	 * versions of the same edit.
	 // If the image dimensions are within 1px of the expected size, use it.

Since thumbnails are square, as I understand they would only get a populated srcset if the original image was also a square.

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 srcset because the other image sizes are also square.

@adamsilverstein
Copy link
Member

adamsilverstein commented Jul 8, 2024

it did get a fully-populated srcset because the other image sizes are also square.

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

<picture class="wp-picture-407" style="display: contents;">
<img width="150" height="150" src="https://wpdev.localhost/wp-content/uploads/2024/07/cat-2-150x150.jpg" class="attachment-150x150 size-150x150" alt="">
</picture>

would become

<picture class="wp-picture-407" style="display: contents;">
<source type="image/avif" src="https://wpdev.localhost/wp-content/uploads/2024/07/cat-2-150x150.avif">
<source type="image/jpeg" src="https://wpdev.localhost/wp-content/uploads/2024/07/cat-2-150x150.jpg">
<img width="150" height="150" src="https://wpdev.localhost/wp-content/uploads/2024/07/cat-2-150x150.jpg" class="attachment-150x150 size-150x150" alt=""></picture>

note the -150x150 suffix, including for the img tag. I'm not sure if the current PR uses that image correctly.

Copy link
Member

@adamsilverstein adamsilverstein left a 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

@westonruter
Copy link
Member

westonruter commented Jul 10, 2024

Punting this to the next release.

@imrraaj
Copy link
Author

imrraaj commented Jul 16, 2024

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 img tag url should also have -150x150 suffix. Am I getting this right?

@mukeshpanchal27
Copy link
Member

@westonruter @adamsilverstein This has been solved in #1354 and merged in trunk. The IMG src check is added in #1408.

Closing this PR now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Bug An existing feature is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Picture element images: Image size issue

4 participants