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
Conversation
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.
@mukeshpanchal27 Please have a look at my comments below.
modules/images/webp-uploads/load.php
Outdated
* @since n.e.x.t | ||
* | ||
* @param bool $preferred_filesize Prioritize file size over mime type. Default true. | ||
*/ |
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.
document @return
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.
Thanks for the feedback, Adam.
I think it should be a param
for the filter, as I checked the similar filters in the Performance plugin and WordPress code, and there it uses param
for the filters.
https://github.com/WordPress/performance/blob/trunk/modules/images/webp-uploads/load.php#L430-L442
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-admin/options.php#L198-L208
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/rest-api/class-wp-rest-server.php#L267-L274
modules/images/webp-uploads/load.php
Outdated
/** | ||
* 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 |
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.
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.
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.
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.
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.
Overall approach seems solid, nice work! I left a few suggestions mostly around filter naming and documentation.
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.
Thanks, @mukeshpanchal27. This is a good start. Left some comments for you to address. Please, take a look.
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.
@mukeshpanchal27 Mostly looks solid to me. I have one suggestion about avoiding duplicate code.
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.
@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.
…name some test name
@felixarntz Thank you for the feedback. I have addressed the feedback and updated the PR and unit tests accordingly. |
Stupid question, what about pngs and gifs? |
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.
@mukeshpanchal27 Pretty much LGTM! Two minor nit-picks that aren't blockers though.
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. |
@adamsilverstein @eugene-manuilov Can you please re-review this PR since you previously requested changes? |
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.
Left one nit pick spacing feedback item on a doc block. Nice work!
01aa49e
to
6aaa947
Compare
Assign it to @felixarntz. It is now ready to merge. |
@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? |
@bethanylang All the changes are addressed, which were raised by @eugene-manuilov. and resolved all conversations. |
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.
LGTM
@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. |
deca626
to
4b00046
Compare
@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 |
Excellent, thanks @mukeshpanchal27! |
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 istrue
in code base. For the unit tests we don't need this filter so addedadd 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 includeadd_filter( 'webp_prioritise_filesize_over_mime_type', '__return_true' );
.Fixes #372
Checklist
[Focus]
orInfrastructure
label.[Type]
label.no milestone
label.