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

Discard WebP image if it is larger than corresponding JPEG image #418

Merged
merged 13 commits into from Jul 14, 2022

Conversation

mukeshpanchal27
Copy link
Member

Summary

After these changes, it affects many current unit tests because those tests are expecting .webp extensions but the images are bigger, so they end up with .jpg extensions. To handle the tests I use the filter approach to pass tests. thanks, @peterwilsoncc for that review.

After implementing this change we no longer need the smaller file size filter and related code. The filter $prefer_smaller_image_file = apply_filters('webp_uploads_prefer_smaller_image_file', false); and code for full size image https://github.com/WordPress/performance/blob/trunk/modules/images/webp-uploads/load.php#L477-L485 and thumbnail size image https://github.com/WordPress/performance/blob/trunk/modules/images/webp-uploads/load.php#L533-L541 because now images have only files that have a smaller size.

Introduce a new filter webp_prioritise_filesize_over_mime_type to handle old unit tests.

The new file size changes affect some old tests to handle it a new filter webp_prioritise_filesize_over_mime_type introduce, that allow users to disable to the new behavior. by default its value is true in code base. For the unit tests we don't need this filter so added add tests_add_filter( 'webp_prioritise_filesize_over_mime_type', '__return_false' ); in the tests bootstrap file so old tests don;t affect with new changes. For new tests, we include add_filter( 'webp_prioritise_filesize_over_mime_type', '__return_true' );.

Fixes #372

Checklist

  • PR has either [Focus] or Infrastructure label.
  • PR has a [Type] label.
  • PR has a milestone or the no milestone label.

@mukeshpanchal27 mukeshpanchal27 added [Type] Enhancement A suggestion for improvement of an existing feature [Focus] Images Issues related to the Images focus area [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels Jul 6, 2022
@mukeshpanchal27 mukeshpanchal27 added this to the 1.3.0 milestone Jul 6, 2022
@mukeshpanchal27 mukeshpanchal27 added this to Backlog in [Focus] Images via automation Jul 6, 2022
@mukeshpanchal27 mukeshpanchal27 self-assigned this Jul 6, 2022
Copy link
Member

@mehulkaklotar mehulkaklotar left a comment

Choose a reason for hiding this comment

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

@mukeshpanchal27 Please have a look at my comments below.

modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Show resolved Hide resolved
tests/bootstrap.php Outdated Show resolved Hide resolved
[Focus] Images automation moved this from Backlog to Review Jul 6, 2022
@mukeshpanchal27 mukeshpanchal27 marked this pull request as ready for review July 6, 2022 10:43
* @since n.e.x.t
*
* @param bool $preferred_filesize Prioritize file size over mime type. Default true.
*/
Copy link
Member

Choose a reason for hiding this comment

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

document @return

Copy link
Member Author

Choose a reason for hiding this comment

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

/**
* Filter whether to prioritize the file type over the mime type.
*
* By default the performance lab plugin will use the mime type with the smaller filesize
Copy link
Member

Choose a reason for hiding this comment

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

This documentation can be improved a bit - the filter controls whether to retain WebP images even if they are larger than the jpegs, right?

Maybe something like
"Filter whether WebP images that are larger than the matching JPEG should be discarded. Default is true"

Also makes me think the filter name should reflect this as well, maybe something like webp_uploads_discard_larger_generated_images, curious what @felixarntz thinks since he is great at naming things.

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 for the feedback. I have updated the documentation for the filter and updated the filter name to webp_uploads_discard_larger_generated_images for now. Will update it when Felix reviews this and suggests the changes regarding the filter name.

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.

Overall approach seems solid, nice work! I left a few suggestions mostly around filter naming and documentation.

Copy link
Contributor

@eugene-manuilov eugene-manuilov left a comment

Choose a reason for hiding this comment

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

Thanks, @mukeshpanchal27. This is a good start. Left some comments for you to address. Please, take a look.

modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
tests/bootstrap.php Outdated Show resolved Hide resolved
tests/modules/images/webp-uploads/load-tests.php Outdated Show resolved Hide resolved
tests/modules/images/webp-uploads/load-tests.php Outdated Show resolved Hide resolved
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mukeshpanchal27 Mostly looks solid to me. I have one suggestion about avoiding duplicate code.

modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/helper.php Outdated Show resolved Hide resolved
modules/images/webp-uploads/load.php Outdated Show resolved Hide resolved
Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mukeshpanchal27 Thank you for the updates. The production code looks good now, except for the small problem with the doc block for the filter.

Now that we have this function, can you add test coverage for it? Probably a good one to use a @dataProvider with different values for the $original and $additional, plus I think it would be good to also have one test to ensure that setting the filter to false disables that behavior.

@mukeshpanchal27
Copy link
Member Author

@felixarntz Thank you for the feedback. I have addressed the feedback and updated the PR and unit tests accordingly.

@spacedmonkey
Copy link
Member

Stupid question, what about pngs and gifs?

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

@mukeshpanchal27 Pretty much LGTM! Two minor nit-picks that aren't blockers though.

tests/modules/images/webp-uploads/helper-tests.php Outdated Show resolved Hide resolved
tests/modules/images/webp-uploads/helper-tests.php Outdated Show resolved Hide resolved
@felixarntz
Copy link
Member

@spacedmonkey

Stupid question, what about pngs and gifs?

The scope of the current project is only to generate WebP for JPEG. For PNG and even more so GIF, there are additional complexities to consider. If someone wanted to generate WebP from PNG (or even JPEG from PNG), they could easily do so using the available filters, and it all would be subject to the same behavior as here. This is in fact a very powerful feature of this "additional MIME type support" feature, that it can also satisfy lots of other potential use-cases with few lines of code. We can always think about changing the defaults and expanding support inside of core later.

@felixarntz
Copy link
Member

felixarntz commented Jul 12, 2022

@adamsilverstein @eugene-manuilov Can you please re-review this PR since you previously requested changes?

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.

Left one nit pick spacing feedback item on a doc block. Nice work!

@mukeshpanchal27
Copy link
Member Author

Assign it to @felixarntz. It is now ready to merge.

@bethanylang
Copy link
Contributor

@mukeshpanchal27 I still see unaddressed changes from @eugene-manuilov and it sounds like @felixarntz wanted him to do a final review, as well – can you take another look?

@mukeshpanchal27
Copy link
Member Author

@bethanylang All the changes are addressed, which were raised by @eugene-manuilov. and resolved all conversations.

Copy link
Contributor

@eugene-manuilov eugene-manuilov left a comment

Choose a reason for hiding this comment

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

LGTM

@felixarntz
Copy link
Member

@mukeshpanchal27 Looks like there is a merge conflict here now that something else got merged. Can you please address this? Then we can merge it.

@mukeshpanchal27
Copy link
Member Author

@felixarntz Merge conflict resolved.

The foreach loop added for the https://github.com/WordPress/performance/blob/trunk/modules/images/webp-uploads/load.php#L625-L633 to get $target_mime.

@felixarntz
Copy link
Member

Excellent, thanks @mukeshpanchal27!

@felixarntz felixarntz merged commit 6164fbe into trunk Jul 14, 2022
@felixarntz felixarntz deleted the fix/372-discard-larger-webp branch July 14, 2022 18:29
[Focus] Images automation moved this from Review to Done Jul 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Focus] Images Issues related to the Images focus area [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Enhancement A suggestion for improvement of an existing feature
Projects
Development

Successfully merging this pull request may close these issues.

Discard WebP image if it is larger than corresponding JPEG image
7 participants