mirror of
https://github.com/go-gitea/gitea.git
synced 2026-03-12 02:24:21 -05:00
Convert avatars to WEBP during upload #10708
Closed
opened 2025-11-02 09:16:11 -06:00 by GiteaMirror
·
20 comments
No Branch/Tag Specified
main
release/v1.25
release/v1.24
release/v1.23
release/v1.22
release/v1.21
release/v1.20
release/v1.19
release/v1.18
release/v1.17
release/v1.16
release/v1.15
release/v1.14
release/v1.13
release/v1.12
release/v1.11
release/v1.10
release/v1.9
release/v1.8
v1.25.3
v1.25.2
v1.25.1
v1.25.0
v1.24.7
v1.25.0-rc0
v1.26.0-dev
v1.24.6
v1.24.5
v1.24.4
v1.24.3
v1.24.2
v1.24.1
v1.24.0
v1.23.8
v1.24.0-rc0
v1.25.0-dev
v1.23.7
v1.23.6
v1.23.5
v1.23.4
v1.23.3
v1.23.2
v1.23.1
v1.23.0
v1.23.0-rc0
v1.24.0-dev
v1.22.6
v1.22.5
v1.22.4
v1.22.3
v1.22.2
v1.22.1
v1.22.0
v1.23.0-dev
v1.22.0-rc1
v1.21.11
v1.22.0-rc0
v1.21.10
v1.21.9
v1.21.8
v1.21.7
v1.21.6
v1.21.5
v1.21.4
v1.21.3
v1.21.2
v1.20.6
v1.21.1
v1.21.0
v1.21.0-rc2
v1.21.0-rc1
v1.20.5
v1.22.0-dev
v1.21.0-rc0
v1.20.4
v1.20.3
v1.20.2
v1.20.1
v1.20.0
v1.19.4
v1.21.0-dev
v1.20.0-rc2
v1.20.0-rc1
v1.20.0-rc0
v1.19.3
v1.19.2
v1.19.1
v1.19.0
v1.19.0-rc1
v1.20.0-dev
v1.19.0-rc0
v1.18.5
v1.18.4
v1.18.3
v1.18.2
v1.18.1
v1.18.0
v1.17.4
v1.18.0-rc1
v1.19.0-dev
v1.18.0-rc0
v1.17.3
v1.17.2
v1.17.1
v1.17.0
v1.17.0-rc2
v1.16.9
v1.17.0-rc1
v1.18.0-dev
v1.16.8
v1.16.7
v1.16.6
v1.16.5
v1.16.4
v1.16.3
v1.16.2
v1.16.1
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c1
v1.3.2
v1.3.1
v1.3.0
v1.3.0-rc2
v1.3.0-rc1
v1.2.3
v1.2.2
v1.2.1
v1.2.0
v1.2.0-rc3
v1.2.0-rc2
v1.1.4
v1.2.0-rc1
v1.1.3
v1.1.2
v1.1.1
v1.1.0
v1.0.2
v1.0.1
v1.0.0
v0.9.99
Labels
Clear labels
$20
$250
$50
$500
backport/done
💎 Bounty
docs-update-needed
good first issue
hacktoberfest
issue/bounty
issue/confirmed
issue/critical
issue/duplicate
issue/needs-feedback
issue/not-a-bug
issue/regression
issue/stale
issue/workaround
lgtm/need 2
modifies/api
modifies/translation
outdated/backport/v1.18
outdated/theme/markdown
outdated/theme/timetracker
performance/bigrepo
performance/cpu
performance/memory
performance/speed
pr/breaking
proposal/accepted
proposal/rejected
pr/wip
pull-request
reviewed/wontfix
💰 Rewarded
skip-changelog
status/blocked
topic/accessibility
topic/api
topic/authentication
topic/build
topic/code-linting
topic/commit-signing
topic/content-rendering
topic/deployment
topic/distribution
topic/federation
topic/gitea-actions
topic/issues
topic/lfs
topic/mobile
topic/moderation
topic/packages
topic/pr
topic/projects
topic/repo
topic/repo-migration
topic/security
topic/theme
topic/ui
topic/ui-interaction
topic/ux
topic/webhooks
topic/wiki
type/bug
type/deprecation
type/docs
type/enhancement
type/feature
type/miscellaneous
type/proposal
type/question
type/refactoring
type/summary
type/testing
type/upstream
Mirrored from GitHub Pull Request
Milestone
No items
No Milestone
Projects
Clear projects
No project
No Assignees
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: github-starred/gitea#10708
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @silverwind on GitHub (Apr 21, 2023).
Feature Description
As discussed in https://github.com/go-gitea/gitea/pull/24248#issuecomment-1518131032 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: https://github.com/go-gitea/gitea/issues/18907
@silverwind commented on GitHub (Apr 21, 2023):
Also, trying to upload a animated WEBP image currently gives this error, likely because the conversion code can not deal with animated WEBP correctly:
@zeripath commented on GitHub (May 10, 2023):
Squaring
hmm... 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:
23ae939ef3/modules/avatar/avatar.go (L47-L86)The squaring happens because of:
23ae939ef3/modules/avatar/avatar.go (L64-L84)Scaling up
Similarly the increase in size occurs because of the call to resize.
23ae939ef3/modules/avatar/avatar.go (L84)Exporting as png
Exporting as png appears to occur in two places:
23ae939ef3/services/repository/avatar.go (L50)23ae939ef3/services/user/user.go (L265)These would both need to change to use webp.
We could probably change the code in generateRandomAvatar to use webp too:
23ae939ef3/models/repo/avatar.go (L51)(It does not look like:
23ae939ef3/tests/integration/user_avatar_test.go (L55)would need to change)
Animation
Unfortunately the golang x/image/png library does not support animation. There has been a feature request:
https://github.com/golang/go/issues/53364
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 Resize
As far as I can see
resize.Resize(and in some casescutter.Crop) will convert the avatar intoimage.RGBAwhich decompresses the image and may make it quite large - taking up memory. This does not seem quite ideal.So I think what we need to do is to rethink about what the avatar code is supposed to do.
@silverwind commented on GitHub (May 10, 2023):
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 https://github.com/golang/go/issues/53364.
@sosasees commented on GitHub (May 10, 2023):
this is not needed.
animated avatars can be fun on a casual website,
but on a serious website like Gitea they would be just annoying.
@silverwind commented on GitHub (May 10, 2023):
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.
@zeripath commented on GitHub (May 10, 2023):
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 though
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.
@silverwind commented on GitHub (May 10, 2023):
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 https://github.com/go-gitea/gitea/issues/8972.
@wxiaoguang commented on GitHub (May 10, 2023):
I guess introducing a full featured webp/png converter would bloat Gitea not just a little ....
@silverwind commented on GitHub (May 10, 2023):
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.
@wxiaoguang commented on GitHub (May 10, 2023):
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.
@silverwind commented on GitHub (May 10, 2023):
I'd certainly not see this necessary for the avatar encoder. png encode is in the stdlib so will be forever stable.
@wxiaoguang commented on GitHub (May 10, 2023):
The problem is
webp......@silverwind commented on GitHub (May 10, 2023):
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.
@wxiaoguang commented on GitHub (May 10, 2023):
After reading the code, I think
png.Encodealready doesdefault compression.png.Encode -> writeIDATs -> e.writeImage(e.bw, e.m, e.cb, levelToZlib(e.enc.CompressionLevel))The
CompressionLeveldefaults to 0, aka DefaultCompression -> zlib.DefaultCompressionBut, the affect is not good?
@silverwind commented on GitHub (May 10, 2023):
I didn't test this, but if https://github.com/go-gitea/gitea/issues/8972#issuecomment-1542513735 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.
@sosasees commented on GitHub (May 10, 2023):
we should downsize to 1024px² instead of 1000px²
so that the downsized images could be rendered slightly faster.
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)
@silverwind commented on GitHub (May 10, 2023):
Good idea, that makes the downsize threshold 1048576 pixels then.
@zeripath commented on GitHub (May 10, 2023):
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) { ... }
23ae939ef3/modules/avatar/avatar.go (L47-L86)23ae939ef3/modules/avatar/avatar.go (L52-L57)Default values are:
23ae939ef3/modules/setting/picture.go (L17-L18)This rejection is important because resize and crop will create RGBA images - so those could be huge byte slices.
23ae939ef3/modules/avatar/avatar.go (L64-L82)23ae939ef3/modules/avatar/avatar.go (L84)23ae939ef3/modules/avatar/avatar.go (L26)So we are currently downsizing/upsizing everything to 290 x 290 pixels.
Proposal
cropcode portionimgCfg.Width > 290 || imgCfg.Height > 290maxDim = max(imgCfg.Width, imgCfg.Height)imgCfg.Width/maxDim*290, imgCfg.Height / maxDim * 290@silverwind commented on GitHub (May 10, 2023):
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
Avatar.RenderedSizeFactorfor this resize, so that would with the default value of 3 make the size 870 above which to resize and to which resize to.Other points sound good.
@wxiaoguang commented on GitHub (May 11, 2023):
-> Improve avatar uploading / resizing / compressing #24653
JPG/PNG/APNG/WEBP all works (for most cases)