-
Notifications
You must be signed in to change notification settings - Fork 107
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
Add WebP for uploads module #32
Conversation
webp-default
module, first passwebp-default
module
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.
Awesome to see the first module! 🎉
Functionality-wise this mostly looks good, left a few comments on implementation details. In addition, I have two higher-level comments:
- I'm not sure about the
webp-default
(WebP default
) name, as it's a bit unprecise. Honestly I also don't have a better idea yet, but let's brainstorm this a bit more? Maybe something likewebp-image-uploads
? - It would be great to add unit tests. The functionality here is super simple, but since this is the first module it might still be good to cover the filter addition call and set a precedence for how to add module tests. It would also allow properly testing the infrastructure from Complete PHPUnit infrastructure for out-of-the-box local and CI testing, including initial tests #29 for modules.
how about
Will do, I'll start with a simple test to verify the filter is added as expected then look at adding a test that actually verifies webp is output when processing a jpeg. |
237fc7e
to
9429e06
Compare
tests/webp-uploads-test.php
Outdated
update_option( PERFLAB_MODULES_SETTING, $new_value ); | ||
perflab_load_active_modules(); | ||
|
||
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { |
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.
would be ideal to run this test against several PHP versions, some that support WebP and some that don't. That way we can validate that the filter is only applied when the system supports WebP.
this check may be overly defensive as I think we do a similar check in core before actually switching, but I thik still worth having.
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.
Agreed. Right now the test suite only runs on latest WordPress and PHP 7.4, maybe let's open a separate issue to evaluate what versions we should run on? We also need to decide in general which WordPress and PHP versions the plugin should support overall, so that could be discussed in the same issue.
tests/webp-uploads-test.php
Outdated
update_option( PERFLAB_MODULES_SETTING, $new_value ); | ||
perflab_load_active_modules(); | ||
|
||
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { |
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.
Agreed. Right now the test suite only runs on latest WordPress and PHP 7.4, maybe let's open a separate issue to evaluate what versions we should run on? We also need to decide in general which WordPress and PHP versions the plugin should support overall, so that could be discussed in the same issue.
1bb7a9c
to
155989b
Compare
155989b
to
aa753c0
Compare
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
Co-authored-by: Felix Arntz <felixarntz@users.noreply.github.com>
cfcfdcf
to
f8c9e2f
Compare
|
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.
🎉
Hello @adamsilverstein. It's great to see this PR merged 🎉. I've made a few tests, and I have a few things to mention. First I've uploaded a medium size JPEG image (width = 1280px) in the media library. The image remains a JPEG in the media library. And I added it in a post. Which output a WebP image in the frontend. I think this is the expected behavior right? For the second test, I uploaded a very large image (width>2560px). So the image have been scaled down to 2560px. And while being scaled down the image have been converted to WebP in the media library. |
if I may interfere, in the past I have already built a filter that saves (and removes) a webp copy of all images uploaded in wordpress (using imagemagik), it works with document previews if they are created by WordPress (as it happens with pdf for example). In my personal experience, compressing jpgs into webp could be just a waste of time and space for users because imho in terms of size and quality these two formats are quite similar and would create only a useless duplication of the same file, while, with the conversion of transparent png the advantage is very huge (about 1/10 of the original file) @JustinyAhin I guess the filename.ext.webp is the most common name variation used for the webp copy, because it's very easy to hijack the request with nginx |
oh, good catch - that looks wrong, might be a bug there. the post meta still records the jpeg mime type. the image is actually a wepb though i expect... i'll take a look. |
Hey @erikyo - thank you sharing your experience and the code snippet, very interesting. I noticed in your code, you used a very high quality setting for creating the images (95 for jpegs for example) - did you try with lower quality?
with this approach, I assume WordPress does not add your WebP images to the post meta sizes array, right? I wonder if we could get WordPress to generate these images instead.
With a setting of 82 quality, the files should be around 30% smaller, that seems worthwhile, especially since the current approach generates the WebP sub sizes instead of the jpeg sub sizes, not in addition to them. I'm curious how your approach works on the front end, do you replace all urls in the default img src/srcset with '.webp'?
right this is why we only create the webp version and serve that.. it might be useful to have extra mime types/files for avif or jpegxl though |
@JustinyAhin - I created a follow up task to investigate the mime type data issue further - #75 |
hi @adamsilverstein,
Yes and for what it looks to me with lower compressions the image was degraded (I can be wrong!) and I preferred a 1:1 ratio between the weight of the jpg and webp (in practice the only advantage is with png where instead i got a about 15% of the original weight). Much later i read the article you posted here, i'll try with these settings, is likely that i was wrong.
Yes, exactly... but i wrote this almost 9 months ago (for a customer with heavy pagespeed issues caused by png images) and in the meantime things have changed. My solution was to was to convert all images with "shadow" webp copies and serve them with nginx if they were available otherwise the original one was served.
You can see how it works in my blog: //modul-r.codekraft.it/... this is an heavily lazy-loaded page (i'm testing my script here). edit: added some info in the last point above |
…s/performance#32 (comment)) To improve the optimization (where possible) I try to use the same compression quality that is used in the jpg so, if this has been compressed with a quality of 50% also the webp will have this quality
hi @adamsilverstein
|
@erikyo good points. Since subsequent image regeneration will use the "full" image, we keep it in the original format. Good point about that being used/loaded by the browser. Maybe we can improve the markup > which theme are you testing with, and which browser?
We are researching the best quality setting to use for WebP in #7 and are also discussing setting quality by mime type (and possibly size) which is not currently the case. |
the theme was "Modul-r" (i am the author) and I use Chrome Canary (Version 99.0.4819.0 (Official Build) canary (64-bit))
It is not essential but adapting the quality of the webp, from what I have been able to test, you can cut an additional 10-20% of the weight of the page depending on the size of the images (larger images = larger gain) |
thanks for the additional details. |
Fixes #4
Also addresses #22
With this module enabled, uploaded JPEG images are saved as WebP sub-sizes.
image_editor_output_format
filter