-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Convert avatars to WEBP during upload #24263
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
Comments
Also, trying to upload a animated WEBP image currently gives this error, likely because the conversion code can not deal with animated WEBP correctly: |
Squaringhmm... as far as I can see the squaring process is unrelated to the choice of format. If you take a look at the code for Prepare: gitea/modules/avatar/avatar.go Lines 47 to 86 in 23ae939
The squaring happens because of: gitea/modules/avatar/avatar.go Lines 64 to 84 in 23ae939
Scaling upSimilarly the increase in size occurs because of the call to resize. gitea/modules/avatar/avatar.go Line 84 in 23ae939
Exporting as pngExporting as png appears to occur in two places: gitea/services/repository/avatar.go Line 50 in 23ae939
Line 265 in 23ae939
These would both need to change to use webp. We could probably change the code in generateRandomAvatar to use webp too: Line 51 in 23ae939
(It does not look like:
would need to change) AnimationUnfortunately the golang x/image/png library does not support animation. There has been a feature request: but no action has been taken. There are other libraries that use cgo to bind to libwebp and should provide animation support but we would need to change from using image.Decode to some other function and it would need to detect if there is animation present. Gitea at present will probably just take the first frame of an animated gif. So I think animation is going to take a bit of work. Issues with Scaling and ResizeAs far as I can see So I think what we need to do is to rethink about what the avatar code is supposed to do.
|
I don't think we need to square or crop. Browser rendering can and should take care of letterboxing the image. We can certainly ensure this in our rendering. If other sites that embed gitea avatars assume our avatars are always square, they will need to fix it themselves.
Yes, browser support is good enough that we can store a well-compressed webp.
Animations are nice to have, but not a requirement for me. Happy to defer this to golang/go#53364. |
this is not needed. |
I guess resizing could be considered above a certain pixel count. Let's say a lazy user directly uploads a photo taken from a digital camera which is let's say 50 Megapixels. We'd definitely want to downsize that to not destroy rendering performance and bandwidth consumption for other users. I would say we could use a Megapixel constant above which to downsize the image to something more suitable for web consumption. Maybe make it 1 Megapixel, which is a 1000x1000 image that is suitable for fine rendering on 4dppx devices down to 250px width/height, which I think is close to avatars rendered on user pages. |
OK looking at encoding directly to webp we can't use x/image/webp and would need to use something that wraps the libwebp C library or write a PR to x/image/webp to do the encoding. The first option I came across: https://github.com/kolesa-team/go-webp requires that I install libwebp-dev on my system - this would likely cause a significant issue with compiling on Windows. PATCH for this option - would need changes to builders to install libwebp-dev thoughdiff --git a/go.mod b/go.mod
index 24765df18..e9e71a27e 100644
--- a/go.mod
+++ b/go.mod
@@ -218,6 +218,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
+ github.com/kolesa-team/go-webp v1.0.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/libdns/libdns v0.2.1 // indirect
diff --git a/go.sum b/go.sum
index 62c01c690..269d46d55 100644
--- a/go.sum
+++ b/go.sum
@@ -792,6 +792,8 @@ github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQ
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/kljensen/snowball v0.6.0/go.mod h1:27N7E8fVU5H68RlUmnWwZCfxgt4POBJfENGMvNRhldw=
+github.com/kolesa-team/go-webp v1.0.4 h1:wQvU4PLG/X7RS0vAeyhiivhLRoxfLVRlDq4I3frdxIQ=
+github.com/kolesa-team/go-webp v1.0.4/go.mod h1:oMvdivD6K+Q5qIIkVC2w4k2ZUnI1H+MyP7inwgWq9aA=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
@@ -1334,6 +1336,7 @@ golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EH
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
+golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/image v0.7.0 h1:gzS29xtG1J5ybQlv0PuyfE3nmc6R4qB73m6LUUmvFuw=
golang.org/x/image v0.7.0/go.mod h1:nd/q4ef1AKKYl/4kft7g+6UyGbdiqWqTP1ZAbRoV7Rg=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
diff --git a/models/repo/avatar.go b/models/repo/avatar.go
index a76a94926..ac961989a 100644
--- a/models/repo/avatar.go
+++ b/models/repo/avatar.go
@@ -6,7 +6,6 @@ package repo
import (
"context"
"fmt"
- "image/png"
"io"
"net/url"
"strings"
@@ -16,6 +15,9 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
+
+ "github.com/kolesa-team/go-webp/encoder"
+ "github.com/kolesa-team/go-webp/webp"
)
// CustomAvatarRelativePath returns repository custom avatar file path.
@@ -48,7 +50,12 @@ func generateRandomAvatar(ctx context.Context, repo *Repository) error {
repo.Avatar = idToString
if err := storage.SaveFrom(storage.RepoAvatars, repo.CustomAvatarRelativePath(), func(w io.Writer) error {
- if err := png.Encode(w, img); err != nil {
+ options, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, 75)
+ if err != nil {
+ log.Error("Unable to create encoder options: %v", err)
+ return err
+ }
+ if err := webp.Encode(w, img, options); err != nil {
log.Error("Encode: %v", err)
}
return err
diff --git a/services/repository/avatar.go b/services/repository/avatar.go
index 74e5de877..9c6dd777a 100644
--- a/services/repository/avatar.go
+++ b/services/repository/avatar.go
@@ -6,7 +6,6 @@ package repository
import (
"context"
"fmt"
- "image/png"
"io"
"strconv"
"strings"
@@ -16,6 +15,9 @@ import (
"code.gitea.io/gitea/modules/avatar"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/storage"
+
+ "github.com/kolesa-team/go-webp/encoder"
+ "github.com/kolesa-team/go-webp/webp"
)
// UploadAvatar saves custom avatar for repository.
@@ -47,7 +49,12 @@ func UploadAvatar(ctx context.Context, repo *repo_model.Repository, data []byte)
}
if err := storage.SaveFrom(storage.RepoAvatars, repo.CustomAvatarRelativePath(), func(w io.Writer) error {
- if err := png.Encode(w, *m); err != nil {
+ options, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, 75)
+ if err != nil {
+ log.Error("Unable to create encoder options: %v", err)
+ return err
+ }
+ if err := webp.Encode(w, *m, options); err != nil {
log.Error("Encode: %v", err)
}
return err
diff --git a/services/user/user.go b/services/user/user.go
index d52a2f404..1b379c025 100644
--- a/services/user/user.go
+++ b/services/user/user.go
@@ -6,7 +6,6 @@ package user
import (
"context"
"fmt"
- "image/png"
"io"
"time"
@@ -25,6 +24,9 @@ import (
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/packages"
+
+ "github.com/kolesa-team/go-webp/encoder"
+ "github.com/kolesa-team/go-webp/webp"
)
// RenameUser renames a user
@@ -262,7 +264,12 @@ func UploadAvatar(u *user_model.User, data []byte) error {
}
if err := storage.SaveFrom(storage.Avatars, u.CustomAvatarRelativePath(), func(w io.Writer) error {
- if err := png.Encode(w, *m); err != nil {
+ options, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, 75)
+ if err != nil {
+ log.Error("Unable to create encoder options: %v", err)
+ return err
+ }
+ if err := webp.Encode(w, *m, options); err != nil {
log.Error("Encode: %v", err)
}
return err
https://github.com/chai2010/webp appears to wholesale import the libwebp source - which should work but we'd have to keep on top of ensuring that it's up to date. https://git.sr.ht/~jackmordaunt/go-libwebp is a Ccgo port of libwebp to go - which could work without cgo - however, the library is marked experimental so is questionable. |
https://github.com/chai2010/webp sounds like the best bet to me, but it still needs CGO, right? I thought we could get rid of CGO eventually, so I doubt that adding a new CGO dependency will be worth it. I guess as a first step, we can go and alter the resizing/cropping mechanism as discussed while still outputting well compressed PNGs, which in turn would solve #8972. |
I guess introducing a full featured webp/png converter would bloat Gitea not just a little .... |
If I recall correctly, gitea already converts all uploaded avatars to PNG, so we do already feature the PNG encoder. Problem is just that the encoding is not doing any compression I think, resulting in unnecessarly huge avatar image sizes. |
IMO CGO is somewhat better than CCGO at the moment, CCGO is not that popular and users will be locked in it (there is a stale PR: "Pure Go SQLIite", IIRC it also uses CCGO ....) Of course CGO is not ideal either, it affects generated executable. A new brief idea: introduce sidecar/external-plugin mechanism: provide a "GiteaImageConvert" server, if users need it, they could deploy it with Gitea together, then Gitea will use it to convert images. If such mechanism can be made general and stable enough, the CustomAvatarServer/PagesServer and more features could be integrated with Gitea while keeping Gitea itself as simple as possible. |
I'd certainly not see this necessary for the avatar encoder. png encode is in the stdlib so will be forever stable. |
The problem is |
Yes, webp is another topic, but i don't think it's one to invest too much time into currently as the size benefits over PNG aren't that great if both are compressed well. Sooner or later, a proper pure golang webp encoder will emerge and then we will use it. |
After reading the code, I think
The But, the affect is not good? |
I didn't test this, but if #8972 (comment) is to be believed, something may be wrong, either the upsizing (that we should remove and only downsize above 1 megapixel) or the compression would be my guess. |
we should downsize to 1024px² instead of 1000px² citation: Make Better Textures, The 'Power Of Two' Rule & Proper Image Dimensions (this is for games but i think it applies to computer images everywhere) |
Good idea, that makes the downsize threshold 1048576 pixels then. |
This PR uses the github.com/chai2010/webp package to encode avatars as webp. Ref go-gitea#24263 Signed-off-by: Andrew Thornton <art27@cantab.net>
OK now I have a PR that will make avatars webp we can look at the resize/crop code. Its helpful first to understand what we already have: func Prepare(data []byte) (*image.Image, error) { ... }gitea/modules/avatar/avatar.go Lines 47 to 86 in 23ae939
gitea/modules/avatar/avatar.go Lines 52 to 57 in 23ae939
Default values are: gitea/modules/setting/picture.go Lines 17 to 18 in 23ae939
This rejection is important because resize and crop will create RGBA images - so those could be huge byte slices.
gitea/modules/avatar/avatar.go Lines 64 to 82 in 23ae939
gitea/modules/avatar/avatar.go Line 84 in 23ae939
gitea/modules/avatar/avatar.go Line 26 in 23ae939
So we are currently downsizing/upsizing everything to 290 x 290 pixels. Proposal
|
I see 290 was chosen because it is the size of the biggest rendered avatar on the user page. But we should actually take into account Other points sound good. |
-> Improve avatar uploading / resizing / compressing #24653 JPG/PNG/APNG/WEBP all works (for most cases) |
This PR uses the git.sr.ht/~jackmordaunt/go-libwebp package to encode avatars as webp. Ref go-gitea#24263 Close go-gitea#24642 Signed-off-by: Andrew Thornton <art27@cantab.net>
…rd module (#24653) Fixes: #8972 Fixes: #24263 And I think it also (partially) fix #24263 (no need to convert) , because users could upload any supported image format if it isn't larger than AVATAR_MAX_ORIGIN_SIZE The main idea: * if the uploaded file size is not larger than AVATAR_MAX_ORIGIN_SIZE, use the origin * if the resized size is larger than the origin, use the origin Screenshots: JPG: <details>  </details> APNG: <details>   </details> WebP (animated) <details>  </details> The only exception: if a WebP image is larger than MaxOriginSize and it is animated, then current `webp` package can't decode it, so only in this case it isn't supported. IMO no need to support such case: why a user would upload a 1MB animated webp as avatar? crazy ..... --------- Co-authored-by: silverwind <me@silverwind.io>
Feature Description
As discussed in #24248 (comment) and below, we should evaluate converting and optimizing all avatars to WEBP. Currently all input formats (WEBP,PNG,JPG,GIF) end up as PNG during the avatar processing which squares them.
Notably, animation via APNG or WEBP input would be nice to have as well because WEBP can do animation, so even APNG should be convertable. Currently this avatar convertion strips APNG animation, would be nice if it can be preserved.
Related old issue: #18907
The text was updated successfully, but these errors were encountered: