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

Account for responsive images being disabled when generating a PICTURE element #1449

Merged
merged 15 commits into from
Aug 19, 2024

Conversation

mukeshpanchal27
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 commented Aug 8, 2024

Summary

Fixes #1448

After the PR:
Screenshot 2024-08-12 at 12 40 00 PM

@mukeshpanchal27 mukeshpanchal27 added [Type] Bug An existing feature is broken [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels Aug 8, 2024
@mukeshpanchal27 mukeshpanchal27 added this to the webp-uploads n.e.x.t milestone Aug 8, 2024
@mukeshpanchal27 mukeshpanchal27 self-assigned this Aug 8, 2024
@mukeshpanchal27 mukeshpanchal27 marked this pull request as ready for review August 8, 2024 11:40
Copy link

github-actions bot commented Aug 8, 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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: mukeshpanchal27 <mukesh27@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: joemcgill <joemcgill@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>
Co-authored-by: felixarntz <flixos90@git.wordpress.org>

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

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Just some small nitpicks here related to my comment on the original issue.

plugins/webp-uploads/picture-element.php Outdated Show resolved Hide resolved
@mukeshpanchal27
Copy link
Member Author

After 7e09a72, The picture element outputs:

Responsive image:

<picture class="wp-picture-12" style="display: contents;">
  <source type="image/webp" 
    srcset="
       http://localhost:8888/wp-content/uploads/2024/08/car-300x169-jpeg.webp 300w, 
       http://localhost:8888/wp-content/uploads/2024/08/car-1024x576-jpeg.webp 1024w, 
       http://localhost:8888/wp-content/uploads/2024/08/car-768x432-jpeg.webp 768w
    " 
    sizes="(max-width: 1080px) 100vw, 1080px"
  >
  <img 
    fetchpriority="high" 
    decoding="async" 
    width="1080" 
    height="608" 
    src="http://localhost:8888/wp-content/uploads/2024/08/car.jpeg" 
    alt="" 
    class="wp-image-12" 
    srcset="
      http://localhost:8888/wp-content/uploads/2024/08/car.jpeg 1080w, 
      http://localhost:8888/wp-content/uploads/2024/08/car-300x169.jpeg 300w, 
      http://localhost:8888/wp-content/uploads/2024/08/car-1024x576.jpeg 1024w, 
      http://localhost:8888/wp-content/uploads/2024/08/car-768x432.jpeg 768w
    "
    sizes="(max-width: 1080px) 100vw, 1080px"
    >
</picture>

No responsive image (use wp_calculate_image_sizes filter):

<picture class="wp-picture-12" style="display: contents;">
  <source type="image/webp" srcset="http://localhost:8888/wp-content/uploads/2024/08/car-jpeg.webp">
  <img 
    fetchpriority="high" 
    decoding="async" 
    width="1080" 
    height="608" 
    src="http://localhost:8888/wp-content/uploads/2024/08/car.jpeg" 
    alt="" 
    class="wp-image-12"
  >
</picture>

@mukeshpanchal27 mukeshpanchal27 changed the title Picture element: Remove picture and source tags when the responsive image disabled Picture element: Return additional mime type IMG >src when the responsive image disabled Aug 12, 2024
@mukeshpanchal27 mukeshpanchal27 changed the title Picture element: Return additional mime type IMG >src when the responsive image disabled Picture element: Return additional mime type IMG>src when the responsive image disabled Aug 12, 2024
plugins/webp-uploads/picture-element.php Outdated Show resolved Hide resolved
plugins/webp-uploads/picture-element.php Outdated Show resolved Hide resolved
plugins/webp-uploads/picture-element.php Outdated Show resolved Hide resolved
plugins/webp-uploads/picture-element.php Outdated Show resolved Hide resolved
plugins/webp-uploads/tests/test-picture-element.php Outdated Show resolved Hide resolved
plugins/webp-uploads/tests/test-picture-element.php Outdated Show resolved Hide resolved
plugins/webp-uploads/tests/test-picture-element.php Outdated Show resolved Hide resolved
@mukeshpanchal27
Copy link
Member Author

@westonruter Thanks for the feedback. After the new helper function the result of single and multi-mime type images are:

Multi Mime

'image/jpeg' => array( 'image/jpeg', 'image/webp', 'image/avif' )

Medium Size
<picture class="wp-picture-43" style="display: contents;">
  <source 
    type="image/avif"
    srcset="http://localhost:8888/wp-content/uploads/2024/08/car-300x169-jpeg.avif"
  >
  <source
    type="image/webp"
    srcset="http://localhost:8888/wp-content/uploads/2024/08/car-300x169-jpeg.webp"
  >
  <img
    fetchpriority="high"
    decoding="async"
    width="300"
    height="169"
    src="http://localhost:8888/wp-content/uploads/2024/08/car-300x169.jpeg"
    alt=""
    class="wp-image-43"
  >
</picture>
Large Size
<picture class="wp-picture-43" style="display: contents;">
  <source
    type="image/avif"
    srcset="http://localhost:8888/wp-content/uploads/2024/08/car-1024x576-jpeg.avif"
  >
  <source
    type="image/webp"
    srcset="http://localhost:8888/wp-content/uploads/2024/08/car-1024x576-jpeg.webp"
  >
  <img
    decoding="async"
    width="1024" 
    height="576" 
    src="http://localhost:8888/wp-content/uploads/2024/08/car-1024x576.jpeg" 
    alt="" 
    class="wp-image-43"
  >
</picture>
Full Size
<picture class="wp-picture-43" style="display: contents;">
  <source 
    type="image/avif" 
    srcset="http://localhost:8888/wp-content/uploads/2024/08/car-jpeg.avif"
  >
  <source
    type="image/webp"
    srcset="http://localhost:8888/wp-content/uploads/2024/08/car-jpeg.webp"
  >
  <img
    decoding="async" 
    width="1080" 
    height="608" 
    src="http://localhost:8888/wp-content/uploads/2024/08/car.jpeg" 
    alt="" 
    class="wp-image-43"
  >
</picture>

Single Mime

'image/jpeg' => array( 'image/jpeg', 'image/avif)

Medium Size
<picture class="wp-picture-46" style="display: contents;">
  <source
    type="image/avif"
    srcset="http://localhost:8888/wp-content/uploads/2024/08/leaves-300x200-jpg.avif"
  >
  <img 
    loading="lazy" 
    decoding="async"
    width="300" 
    height="200" 
    src="http://localhost:8888/wp-content/uploads/2024/08/leaves-300x200.jpg" 
    alt="" 
    class="wp-image-46"
  >
</picture>
Large Size
<picture class="wp-picture-46" style="display: contents;">
  <source 
    type="image/avif" 
    srcset="http://localhost:8888/wp-content/uploads/2024/08/leaves-1024x683-jpg.avif"
  >
  <img 
    loading="lazy" 
    decoding="async" 
    width="1024" 
    height="683" 
    src="http://localhost:8888/wp-content/uploads/2024/08/leaves-1024x683.jpg" 
    alt="" 
    class="wp-image-46"
  >
</picture>
Full Size
<picture class="wp-picture-46" style="display: contents;">
  <source
    type="image/avif"
    srcset="http://localhost:8888/wp-content/uploads/2024/08/leaves-jpg.avif"
  >
  <img
    loading="lazy" 
    decoding="async" 
    width="1080" 
    height="720" 
    src="http://localhost:8888/wp-content/uploads/2024/08/leaves.jpg" 
    alt="" 
    class="wp-image-46"
  >
</picture>

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: in #1449 (comment) you show examples of how this works when different image sizes are requested, but how is the size being selected for the alternative mime types? I don't see how the size selection works.

plugins/webp-uploads/helper.php Outdated Show resolved Hide resolved
plugins/webp-uploads/helper.php Outdated Show resolved Hide resolved
plugins/webp-uploads/helper.php Outdated Show resolved Hide resolved
if ( isset( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) {
foreach ( $metadata['sizes'] as $size => $size_data ) {

if ( empty( $size_data['file'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid using empty() here?

plugins/webp-uploads/helper.php Show resolved Hide resolved
@mukeshpanchal27
Copy link
Member Author

Curious: in #1449 (comment) you show examples of how this works when different image sizes are requested, but how is the size being selected for the alternative mime types? I don't see how the size selection works.

@westonruter When we disabled the responsive images then it will return only src for the image with size( Large, Medium , Full ) that we selected. #1449 (comment) i share the result for each image size.

@westonruter
Copy link
Member

I'm not actively familiar enough with the internals of image handling in WP without taking time to dive in deeper. I'll defer to @joemcgill and @adamsilverstein who have more experience to finish the reviews.

@westonruter
Copy link
Member

Punting from the 3.4.0 release since we're out of time. If this does get reviewed by Monday morning, make sure the PR's base branch is updated from trunk to release/3.4.0.

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.

Nice fix!

@felixarntz
Copy link
Member

IMO too big of a change to merge now 1 hour before release. I'd say let's get this into the next one.

@westonruter
Copy link
Member

Agreed. Merging into trunk and not into the release branch.

@westonruter westonruter dismissed joemcgill’s stale review August 19, 2024 15:58

Requested changes addressed

@westonruter westonruter changed the title Picture element: Return additional mime type IMG>src when the responsive image disabled Account for responsive images being disabled when generating a PICTURE element Aug 19, 2024
@westonruter westonruter merged commit a3835ea into trunk Aug 19, 2024
18 checks passed
@westonruter westonruter deleted the fix/follow-up-1399 branch August 19, 2024 16:04
@westonruter
Copy link
Member

@mukeshpanchal27 I updated the PR title to be more clear. Please review and amend further as needed.

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: Return additional mime type IMG>src when the responsive image disabled
5 participants