-
Notifications
You must be signed in to change notification settings - Fork 33
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 and avif conversion to strapi-plugin-upload #47
Open
Antoine-lb
wants to merge
4
commits into
strapi:main
Choose a base branch
from
Antoine-lb:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+76
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
- Start Date: 2022-08-16 | ||
- RFC PR: (leave this empty) | ||
|
||
# Summary | ||
|
||
Add the option to convert all uploaded images to WebP or AVIF instead of just resizing them. This would be done via a **opt-in** parameter in `./config/plugins.js`. | ||
|
||
# Motivation | ||
|
||
WebP and AVIF are superior formats for the web and support is growing very fast. | ||
|
||
It got to a point where some projects could decide to **only** work with these formats to improuve the user experience without adding engeneering complexity or hosting costs. | ||
|
||
One could even argue that this could make your service more ecologically friendly, by reducing the data transfered on every page load. | ||
|
||
### How much better? | ||
|
||
"WebP typically achieves an average of 30% more compression than JPEG" -- [Google WebP FAQ](https://developers.google.com/speed/webp/faq#:~:text=WebP%20typically%20achieves%20an%20average,help%20make%20the%20web%20faster.) | ||
|
||
- [Comparative study of WebP, JPEG and JPEG 2000, September 2010 - by Google](https://developers.google.com/speed/webp/docs/c_study) | ||
- [WebP Compression Study - by Google](https://developers.google.com/speed/webp/docs/webp_studyhttps://developers.google.com/speed/webp/docs/webp_studyhttps://developers.google.com/speed/webp/docs/webp_study) | ||
- [WebP vs AVIF - by avif.org](https://avif.io/blog/comparisons/avif-vs-webp/) | ||
|
||
### How good is browser support? | ||
|
||
As of Agust 2022 of the major browsers that don't support it are: | ||
- WebP: Only IE does not support it | ||
- AVIF: Edge and other smaller browsers don't have support, and Safari desktop has partial support | ||
|
||
Sources: | ||
- [https://caniuse.com/webp](https://caniuse.com/webp) | ||
- [https://caniuse.com/avif](https://caniuse.com/avif) | ||
|
||
# Detailed design | ||
|
||
This could be done directly in [/packages/core/upload/server/services/image-manipulation.js](https://github.com/strapi/strapi/blob/master/packages/core/upload/server/services/image-manipulation.js) file. | ||
|
||
A member of the community already had a prototype (no affiliation): | ||
https://github.com/devgar/strapi/blob/243d914efeebfdb44b236af76689c30b73fed646/packages/strapi-plugin-upload/services/image-manipulation.js | ||
|
||
Basically it would instruct `sharp` package to transform to `webp` or `avif`. This would **not** require an aditional dependency, not even a new import in the file. | ||
|
||
It will of course still resizes images like before (e.g. `large`, `medium` ...). | ||
|
||
These changes being applied only at the upload phase, would not require all the images to be reformatted making it pretty safe for retro-compatibility. And it can be turned on and off without much consequences. | ||
|
||
# Example | ||
|
||
Something like this: | ||
```ts | ||
// path: ./config/plugins.ts | ||
|
||
export default ({ env }) => ({ | ||
upload: { | ||
config: { | ||
convertAllImagesTo: 'webp' | 'avif'; // To be defined | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
# Tradeoffs | ||
|
||
- Even if I'm pretty confident that is a good decision for many of us, it is **opinionated**. This could be mitigated by adding more formats as targets like `png` or `jpeg`. | ||
- I don't know how computer intensive it is to do the transformation. | ||
|
||
# Alternatives | ||
|
||
I guess this could be done in a plugin. But this would require replacing the official `strapi-plugin-update` for a community one. This plugin is quite big and important, I wouldn't recommend replacing it with a community-maintained plugin. | ||
|
||
If is not in the official one, I wouldn't bother doing it. | ||
|
||
# Unresolved questions | ||
|
||
- Should it support many different formats (e.g. `png` and `jpeg`) or just work with `webp` and `avif` which are the ones that should be used? More formats would make it less opinionated but would add more maintenance, and may not be that relevant. | ||
- How should the `./config/plugins.ts` look like? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you have some resource one which conversions are supported to webp? is it jpg/png to webp or are there more formats supported?
How does sharp handle non compatible formats ? is it just not doing the transform or throws ? if so we would need to have a mapping to maintain?