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
Avoid requesting an Avatar image bigger than what the template asks for #17422
Conversation
Very curious, how is the The DefaultAvatarPixelSize and AvatarRenderedSizeFactor are just moved from old code, I have no idea why these two values were chosen ... |
@wxiaoguang as reported in #16287 I think it came from a request of 290 as the default size and a factor of 4. Now I see that the 290 value is STILL in modules/avatar/avatar.go (AvatarSize) and is DIFFERENT from the DefaultAvatarPixelSize that I just changed from 28 to 128 in models/avatars/avatar.go -- I'm not sure what uses which of the two values, and if one should just be discarded/dropped |
OMG, I think we should refactor |
Indeed, I don't even understand why would we want to use a AvatarRenderedSizeFactor All I know is that we do NOT want a 1160x1160 image fetched from avatar services by the browsers of the poor Gitea users |
I'm seeing that AvatarSize is used for the generated avatars |
We need more time to think about the avatar size problem. And don't worry, the URL |
The reason for AvatarRenderedSizeFactor, I guess it makes the displayed image clearer on a 2K or 4K Hi-DPI monitor. |
It depends on the service. avatars.kbt.io is a service I control, so I know it won't return a bigger image, but other services (it's a federated service) could return a bigger one. What happens on my service is that if someone asks for an avatar bigger than the biggest image I have of my avatar, it returns that silly drawing instead of a picture of myself, that's why I noticed it :) |
Yes, that's written in the comment, but I'm not using a Hi-DPI monitor so I'd think I should NOT get the SizeFactor applied. It should ideally be decided by JavaScript if one needs the scale applied or not, shouldn't it ? |
Theoretically, yes. But actually, many links are generated by backend code, which do not have knowledge about the frontend information (eg: what display device you are using). So I think the avatar size problem is not an easy problem (and not a serious problem yet), we need think more about it and make a global plan. |
I'd say it's more important to get a 290x290 avatar (as the old times) and see it "pixelated" on 4K monitors than download a huge image (or wrong image, in my case) on a normal monitor. |
Do you have a screenshot of how those pages look on a 4K monitor, btw ? |
Agree, but we only need to change
No. Actually, for myself, I do not care about the image quality of avatars ... but the code was here, I just happened to have done a refactoring. About the |
Oh, by the way, maybe we can introduce another const: MaxAvatarSize , then we can limit the requested size not larger than MaxAvatarSize. It's unreasonable to request a much bigger avatar image indeed. |
For the general plan, how about using a cookie to store wheter the user is using a 4K monitor and only then apply the ScaleFactor ? |
That's too complex, it sounds like we want to kill a chicken with a dragon sword. |
The max size could be set since only serval places to reference the avatar. The biggest one is in user's profile page which is 260x260, with the border/padding/margin, it's 290x290. So I think we should change it to 260 but not 290. And 4x260=1040 is the size for Hi-DPI monitor. We could detect the monitor at frontend, we can check |
I just tested with GitHub, it's not that complex, I don't think we should use window.devicePixelRatio for this simple purpose. For GitHub, it doesn't care about devicePixelRatio (it can be simulated in Chrome DevTools) Small avatars For large avatars, We can just follow the main stream designs, instead of re-inventing the wheels. |
models/avatars/avatar.go
Outdated
@@ -19,10 +19,10 @@ import ( | |||
) | |||
|
|||
// DefaultAvatarPixelSize is the default size in pixels of a rendered avatar | |||
const DefaultAvatarPixelSize = 28 | |||
const DefaultAvatarPixelSize = 128 |
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 const shouldn't be changed. It is used for small avatars.
models/avatars/avatar.go
Outdated
|
||
// AvatarRenderedSizeFactor is the factor by which the default size is increased for finer rendering | ||
const AvatarRenderedSizeFactor = 4 | ||
const AvatarRenderedSizeFactor = 2 |
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 one seems fine. L-G-T-M.
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.
But I think it just has been changed in #15941 from 2 to 4. @silverwind
Unfortunately a x2 factor is still not good for me: And it still doesnt' make sense to multiply by 2 if the HTML tag keeps the original size anyway (check the generated HTML). |
As mentioned by lunny, the AvatarRenderedSizeFactor was changed by silverwind from 2 to 4 before, I think we should make a general agreement for the chosen value. Otherwise there will be more time spent on discussions. I prefer to follow the GitHub way instead of re-inventing wheels.
|
I disagree on reducing What instead should be done is have the backend output dynamic image sizes based on what the browser requests by URL, e.g. |
https://github.com/disintegration/imaging might be useful for resizing, but it certainly can not be done on-the-fly on every request for performance reasons. One solution may be to maintain a disk-based cache of the commonly used sizes and regenerate it when a user changes their avatar. |
I think GitHub either resizes all avatars to 460px on save or stores them in higher resolution and just defaults to HTTP endpoint to 460px when no size argument is given. Generally, I think we don't need not special-case the avatar on the user page. It's just another avatar image that is bigger than others, but not special in any other way. Resizing (to factor*280) and/or optimizing (pngcrush or zopflipng) avatar on save might be a good future change to prevent overly large avatars from eating bandwidth. |
Save a bit of bandwidth by only requesting 3-times the rendered avatar size. Factor 4 is only really beneficial on a handful of mobile phones and I don't think they are the primary device we design for. Fixes: go-gitea#17422 Fixes: go-gitea#16287
Signed-off-by: Andrew Thornton <art27@cantab.net>
Codecov Report
@@ Coverage Diff @@
## main #17422 +/- ##
==========================================
+ Coverage 45.28% 45.30% +0.01%
==========================================
Files 820 820
Lines 90932 90932
==========================================
+ Hits 41179 41195 +16
+ Misses 43194 43184 -10
+ Partials 6559 6553 -6
Continue to review full report at Codecov.
|
Make avatar size factor a configurable option
Thanks @zeripath - I've merged your PR |
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.
I'm going to approve this as it's now configurable.
I don't mind which setting is used but 4 is almost certainly too high and 1 may be too low.
However, in the interests of getting this merged I will approve.
Can we make default more close to current behavior, for example |
Yes, default should be |
I put a pr against @silverwind's to add configuration - I'd be happy to approve that or if you want to make the suggestions against this pr to make the default 3 I think that would be acceptable to. |
#17951 is ready again. |
Save a bit of bandwidth by only requesting 3-times the rendered avatar size. Factor 4 is only really beneficial on a handful of mobile phones and I don't think they are the primary device we design for. Configurability contributed by zeripath. Fixes: go-gitea#17422 Fixes: go-gitea#16287
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.
Blocking merge as the consensus seems to be factor 3 is a good default. #17951 replaces this PR.
Oh, I see the different, then we use #17951 😊 |
Save a bit of bandwidth by only requesting 3-times the rendered avatar size. Factor 4 is only really beneficial on a handful of mobile phones and I don't think they are the primary device we design for. Configurability contributed by zeripath. Fixes: #17422 Fixes: #16287 Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Thanks guys ! |
…17951) Save a bit of bandwidth by only requesting 3-times the rendered avatar size. Factor 4 is only really beneficial on a handful of mobile phones and I don't think they are the primary device we design for. Configurability contributed by zeripath. Fixes: go-gitea#17422 Fixes: go-gitea#16287 Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Hopefully fixes #16287
It makes NO sense to request an avatar of 1160x1160 when it is rendered MUCH MUCH smaller.
See https://try.gitea.io/strk and right-click -> open image in new tab, then check the URL, is formed with
?size=1160
now. I think this PR makes it ask for a size=256 instead (default 128, sizeFactor 2)