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

Use correct sizes for small images #1252

Merged
merged 24 commits into from
Jun 25, 2024

Conversation

mukeshpanchal27
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 commented May 28, 2024

Summary

Fixes: #1251

For initial work:

  • Only return a smaller image size if the content or wide size is larger than the image size.
    • If content size is 620px and content use thumbnail the the size will use thumbnail 150px image size instead of 620px.
  • Parse the width value for "px"; for other types( vw, min() ), return the image width according to the default WordPress behaviour.
@mukeshpanchal27 mukeshpanchal27 added [Type] Enhancement A suggestion for improvement of an existing feature [Focus] Images no milestone PRs that do not have a defined milestone for release [Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin labels May 28, 2024
@mukeshpanchal27 mukeshpanchal27 self-assigned this May 28, 2024
@joemcgill
Copy link
Member

Thanks @mukeshpanchal27. It looks like the intent of this PR to constrain the width attribute value by the layout width. Is that correct? Is this related to an existing issue, or should a new issue be created to associate this PR with?

@joemcgill joemcgill linked an issue May 29, 2024 that may be closed by this pull request
Base automatically changed from fix/1187-accurate-sizes-image-cover-block to feature/more-accurate-sizes-attribute June 5, 2024 04:18
@mukeshpanchal27 mukeshpanchal27 marked this pull request as ready for review June 5, 2024 10:04
Copy link

github-actions bot commented Jun 5, 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>

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

@mukeshpanchal27
Copy link
Member Author

@joemcgill The PR is ready for review.

plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
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.

After discussion with @mukeshpanchal27 and reviewing this PR in more detail, I've left the following comment on the original issue with suggestions for a different approach that we should take to resolve this issue.

@mukeshpanchal27
Copy link
Member Author

@joemcgill @westonruter The PR is ready for review.

As discussed in this comment, I have updated the PR to add custom attributes to the image and cover blocks. These custom attributes are then removed in the wp_content_img_tag filter, which allows us to get both the alignment and image width.

@joemcgill In this commit, I have removed the multiple parsing and added an early check to skip if the custom attribute is not present.

plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
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.

I've tested locally, and this seems to be working as expected for most of the use cases we're trying to solve with this PR. I've left some code quality feedback that would be good to address before merging.

plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
plugins/auto-sizes/hooks.php Outdated Show resolved Hide resolved
Comment on lines 28 to 29
// Disable lazy loading attribute.
add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
Copy link
Member

Choose a reason for hiding this comment

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

This is a risky filter to disable while running these tests, because we'll be missing scenarios where the loading attribute changes the way the markup gets created. I assume you did this to avoid having auto sizes added while running these tests? If so, instead of removing the wp_img_tag_add_loading_attr filter, I would suggest running these without the auto-sizes filter, which seems to be what is causing potential failures here.

Suggested change
// Disable lazy loading attribute.
add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
// Disable auto sizes in these tests.
remove_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' );

However, since the hooks global won't get reset after this class runs, you'll also need to add this hook back after the class runs, or else it will affect any tests that run afterwards. Ex:

	public static function tear_down_after_class(): void {
		parent::tear_down_after_class();

		add_filter( 'wp_content_img_tag', 'auto_sizes_update_content_img_tag' );
	}

If there are specific tests where you need to disable lazy-loading, it would be better to modify hooks directly in that test, because then they will automatically get reset during the WP_UnitTestCase_Base::tear_down() method that runs after each test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @joemcgill for suggestion. This will not working as the filter is re-added through the_content filter so it show error in tests.

1) Tests_Improve_Sizes::test_image_block_with_full_alignment with data set "Return wideSize 1280px instead of medium size 300px" ('medium')
Failed asserting that '<figure class="wp-block-image size-medium"><img loading="lazy" decoding="async" width="300" height="200"   src="http://localhost:8889/wp-content/uploads/2024/06/leaves-283-300x200.jpg" alt="" class="wp-image-5" srcset="http://localhost:8889/wp-content/uploads/2024/06/leaves-283-300x200.jpg 300w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-1024x683.jpg 1024w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-768x512.jpg 768w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283.jpg 1080w" sizes="auto, 100vw" /></figure>' contains "sizes="100vw" ".

/var/www/html/wp-content/plugins/performance/plugins/auto-sizes/tests/test-improve-sizes.php:53

2) Tests_Improve_Sizes::test_image_block_with_full_alignment with data set "Return wideSize 1280px instead of large size 1024px" ('large')
Failed asserting that '<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="683"   src="http://localhost:8889/wp-content/uploads/2024/06/leaves-283-1024x683.jpg" alt="" class="wp-image-5" srcset="http://localhost:8889/wp-content/uploads/2024/06/leaves-283-1024x683.jpg 1024w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-300x200.jpg 300w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-768x512.jpg 768w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283.jpg 1080w" sizes="auto, 100vw" /></figure>' contains "sizes="100vw" ".

/var/www/html/wp-content/plugins/performance/plugins/auto-sizes/tests/test-improve-sizes.php:53

3) Tests_Improve_Sizes::test_image_block_with_full_alignment with data set "Return wideSize 1280px instead of full size 1080px" ('full')
Failed asserting that '<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="1080" height="720"   src="http://localhost:8889/wp-content/uploads/2024/06/leaves-283.jpg" alt="" class="wp-image-5" srcset="http://localhost:8889/wp-content/uploads/2024/06/leaves-283.jpg 1080w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-300x200.jpg 300w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-1024x683.jpg 1024w, http://localhost:8889/wp-content/uploads/2024/06/leaves-283-768x512.jpg 768w" sizes="auto, 100vw" /></figure>' contains "sizes="100vw" ".

/var/www/html/wp-content/plugins/performance/plugins/auto-sizes/tests/test-improve-sizes.php:53

plugins/auto-sizes/tests/improve-sizes-test.php Outdated Show resolved Hide resolved
plugins/auto-sizes/tests/improve-sizes-test.php Outdated Show resolved Hide resolved
@mukeshpanchal27
Copy link
Member Author

@joemcgill @westonruter Thanks for the feedback. I have updated the PR as per your directions. It is now ready for review.

… fix/small-image-sizes

* 'trunk' of https://github.com/WordPress/performance: (113 commits)
  Further refine command explaining type casting
  Elaborate on comment explaining why type casting is added
  Add try/catch block around imagecolorsforindex()
  Fix grammar typos
  Fix static analysis issues in Test_WebP_Uploads_Image_Edit
  Eliminate checks for seemingly impossible false return values
  Update PHPStan to 1.11.5
  Add missing '@'
  Apply suggestions from code review
  Update testcase
  Fix wp:gallery styling not applying
  Update banner image
  Extend check code for WordPress 6.6
  Harmonize return descriptions
  Bump Optimization Detective 0.3.1
  Log URL metrics group collection to console when WP_DEBUG
  Include non-intersecting elements in URL metrics
  Bump version to 0.1.1
  Fix logic error for preg_match() return value
  Add failing test case for a non-background image style
  ...
…mage-sizes

* feature/more-accurate-sizes-attribute:
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.

LGTM

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.

This is working as expected. I reverted the changes to the composer.lock file that were added because I think they were unintentional and doesn't seem to be related to this change. If that was an incorrect assumption we can update composer dependencies in a follow-up task.


return $processor->get_updated_html();
}
// Run filter prior to auto sizes "auto_sizes_update_content_img_tag" filter.
Copy link
Member

Choose a reason for hiding this comment

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

💖

@joemcgill joemcgill merged commit 9e4ee2b into feature/more-accurate-sizes-attribute Jun 25, 2024
14 checks passed
@joemcgill joemcgill deleted the fix/small-image-sizes branch June 25, 2024 22:17
@westonruter westonruter removed the no milestone PRs that do not have a defined milestone for release label Jul 10, 2024
@westonruter westonruter added this to the auto-sizes 1.1.0 milestone Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin [Type] Enhancement A suggestion for improvement of an existing feature
4 participants