Skip to content

Commit cc129d2

Browse files
Make AvatarRenderedSizeFactor configurable and set it to 3 (#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 <[email protected]>
1 parent e78ee73 commit cc129d2

File tree

7 files changed

+22
-16
lines changed

7 files changed

+22
-16
lines changed

custom/conf/app.example.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,10 @@ PATH =
15871587
;AVATAR_MAX_WIDTH = 4096
15881588
;AVATAR_MAX_HEIGHT = 3072
15891589
;;
1590+
;; The multiplication factor for rendered avatar images.
1591+
;; Larger values result in finer rendering on HiDPI devices.
1592+
;AVATAR_RENDERED_SIZE_FACTOR = 3
1593+
;;
15901594
;; Maximum allowed file size for uploaded avatars.
15911595
;; This is to limit the amount of RAM used when resizing the image.
15921596
;AVATAR_MAX_FILE_SIZE = 1048576

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ Define allowed algorithms and their minimum key length (use -1 to disable a type
710710
- `AVATAR_MAX_WIDTH`: **4096**: Maximum avatar image width in pixels.
711711
- `AVATAR_MAX_HEIGHT`: **3072**: Maximum avatar image height in pixels.
712712
- `AVATAR_MAX_FILE_SIZE`: **1048576** (1Mb): Maximum avatar image file size in bytes.
713+
- `AVATAR_RENDERED_SIZE_FACTOR`: **3**: The multiplication factor for rendered avatar images. Larger values result in finer rendering on HiDPI devices.
713714

714715
- `REPOSITORY_AVATAR_STORAGE_TYPE`: **default**: Storage type defined in `[storage.xxx]`. Default is `default` which will read `[storage]` if no section `[storage]` will be a type `local`.
715716
- `REPOSITORY_AVATAR_UPLOAD_PATH`: **data/repo-avatars**: Path to store repository avatar image files.

models/avatars/avatar.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ import (
2121
// DefaultAvatarPixelSize is the default size in pixels of a rendered avatar
2222
const DefaultAvatarPixelSize = 28
2323

24-
// AvatarRenderedSizeFactor is the factor by which the default size is increased for finer rendering
25-
const AvatarRenderedSizeFactor = 4
26-
2724
// EmailHash represents a pre-generated hash map (mainly used by LibravatarURL, it queries email server's DNS records)
2825
type EmailHash struct {
2926
Hash string `xorm:"pk varchar(32)"`

modules/repository/commits.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
user_model "code.gitea.io/gitea/models/user"
1414
"code.gitea.io/gitea/modules/git"
1515
"code.gitea.io/gitea/modules/log"
16+
"code.gitea.io/gitea/modules/setting"
1617
api "code.gitea.io/gitea/modules/structs"
1718
)
1819

@@ -141,7 +142,7 @@ func (pc *PushCommits) AvatarLink(email string) string {
141142
return avatar
142143
}
143144

144-
size := avatars.DefaultAvatarPixelSize * avatars.AvatarRenderedSizeFactor
145+
size := avatars.DefaultAvatarPixelSize * setting.Avatar.RenderedSizeFactor
145146

146147
u, ok := pc.emailUsers[email]
147148
if !ok {

modules/repository/commits_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ func TestPushCommits_AvatarLink(t *testing.T) {
124124
}
125125

126126
assert.Equal(t,
127-
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon&s=112",
127+
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon&s=84",
128128
pushCommits.AvatarLink("[email protected]"))
129129

130130
assert.Equal(t,
131131
"https://secure.gravatar.com/avatar/"+
132132
fmt.Sprintf("%x", md5.Sum([]byte("[email protected]")))+
133-
"?d=identicon&s=112",
133+
"?d=identicon&s=84",
134134
pushCommits.AvatarLink("[email protected]"))
135135
}
136136

modules/setting/picture.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ var (
1818
Avatar = struct {
1919
Storage
2020

21-
MaxWidth int
22-
MaxHeight int
23-
MaxFileSize int64
21+
MaxWidth int
22+
MaxHeight int
23+
MaxFileSize int64
24+
RenderedSizeFactor int
2425
}{
25-
MaxWidth: 4096,
26-
MaxHeight: 3072,
27-
MaxFileSize: 1048576,
26+
MaxWidth: 4096,
27+
MaxHeight: 3072,
28+
MaxFileSize: 1048576,
29+
RenderedSizeFactor: 3,
2830
}
2931

3032
GravatarSource string
@@ -55,6 +57,7 @@ func newPictureService() {
5557
Avatar.MaxWidth = sec.Key("AVATAR_MAX_WIDTH").MustInt(4096)
5658
Avatar.MaxHeight = sec.Key("AVATAR_MAX_HEIGHT").MustInt(3072)
5759
Avatar.MaxFileSize = sec.Key("AVATAR_MAX_FILE_SIZE").MustInt64(1048576)
60+
Avatar.RenderedSizeFactor = sec.Key("AVATAR_RENDERED_SIZE_FACTOR").MustInt(3)
5861

5962
switch source := sec.Key("GRAVATAR_SOURCE").MustString("gravatar"); source {
6063
case "duoshuo":

modules/templates/helper.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,17 +557,17 @@ func Avatar(item interface{}, others ...interface{}) template.HTML {
557557

558558
switch t := item.(type) {
559559
case *user_model.User:
560-
src := t.AvatarLinkWithSize(size * avatars.AvatarRenderedSizeFactor)
560+
src := t.AvatarLinkWithSize(size * setting.Avatar.RenderedSizeFactor)
561561
if src != "" {
562562
return AvatarHTML(src, size, class, t.DisplayName())
563563
}
564564
case *models.Collaborator:
565-
src := t.AvatarLinkWithSize(size * avatars.AvatarRenderedSizeFactor)
565+
src := t.AvatarLinkWithSize(size * setting.Avatar.RenderedSizeFactor)
566566
if src != "" {
567567
return AvatarHTML(src, size, class, t.DisplayName())
568568
}
569569
case *models.Organization:
570-
src := t.AsUser().AvatarLinkWithSize(size * avatars.AvatarRenderedSizeFactor)
570+
src := t.AsUser().AvatarLinkWithSize(size * setting.Avatar.RenderedSizeFactor)
571571
if src != "" {
572572
return AvatarHTML(src, size, class, t.AsUser().DisplayName())
573573
}
@@ -596,7 +596,7 @@ func RepoAvatar(repo *repo_model.Repository, others ...interface{}) template.HTM
596596
// AvatarByEmail renders avatars by email address. args: email, name, size (int), class (string)
597597
func AvatarByEmail(email string, name string, others ...interface{}) template.HTML {
598598
size, class := parseOthers(avatars.DefaultAvatarPixelSize, "ui avatar image", others...)
599-
src := avatars.GenerateEmailAvatarFastLink(email, size*avatars.AvatarRenderedSizeFactor)
599+
src := avatars.GenerateEmailAvatarFastLink(email, size*setting.Avatar.RenderedSizeFactor)
600600

601601
if src != "" {
602602
return AvatarHTML(src, size, class, name)

0 commit comments

Comments
 (0)