@@ -17,8 +17,7 @@ import (
17
17
"code.gitea.io/gitea/modules/avatar/identicon"
18
18
"code.gitea.io/gitea/modules/setting"
19
19
20
- "github.com/nfnt/resize"
21
- "github.com/oliamb/cutter"
20
+ "golang.org/x/image/draw"
22
21
23
22
_ "golang.org/x/image/webp" // for processing webp images
24
23
)
@@ -81,28 +80,10 @@ func processAvatarImage(data []byte, maxOriginSize int64) ([]byte, error) {
81
80
}
82
81
83
82
// try to crop and resize the origin image if necessary
84
- if imgCfg .Width != imgCfg .Height {
85
- var newSize , ax , ay int
86
- if imgCfg .Width > imgCfg .Height {
87
- newSize = imgCfg .Height
88
- ax = (imgCfg .Width - imgCfg .Height ) / 2
89
- } else {
90
- newSize = imgCfg .Width
91
- ay = (imgCfg .Height - imgCfg .Width ) / 2
92
- }
93
-
94
- img , err = cutter .Crop (img , cutter.Config {
95
- Width : newSize ,
96
- Height : newSize ,
97
- Anchor : image.Point {X : ax , Y : ay },
98
- })
99
- if err != nil {
100
- return nil , err
101
- }
102
- }
83
+ img = cropSquare (img )
103
84
104
- targetSize := uint ( DefaultAvatarSize * setting .Avatar .RenderedSizeFactor )
105
- img = resize . Resize ( targetSize , targetSize , img , resize . Bilinear )
85
+ targetSize := DefaultAvatarSize * setting .Avatar .RenderedSizeFactor
86
+ img = scale ( img , targetSize , targetSize , draw . BiLinear )
106
87
107
88
// try to encode the cropped/resized image to png
108
89
bs := bytes.Buffer {}
@@ -124,3 +105,35 @@ func processAvatarImage(data []byte, maxOriginSize int64) ([]byte, error) {
124
105
func ProcessAvatarImage (data []byte ) ([]byte , error ) {
125
106
return processAvatarImage (data , setting .Avatar .MaxOriginSize )
126
107
}
108
+
109
+ // scale resizes the image to width x height using the given scaler.
110
+ func scale (src image.Image , width , height int , scale draw.Scaler ) image.Image {
111
+ rect := image .Rect (0 , 0 , width , height )
112
+ dst := image .NewRGBA (rect )
113
+ scale .Scale (dst , rect , src , src .Bounds (), draw .Over , nil )
114
+ return dst
115
+ }
116
+
117
+ // cropSquare crops the largest square image from the center of the image.
118
+ // If the image is already square, it is returned unchanged.
119
+ func cropSquare (src image.Image ) image.Image {
120
+ bounds := src .Bounds ()
121
+ if bounds .Dx () == bounds .Dy () {
122
+ return src
123
+ }
124
+
125
+ var rect image.Rectangle
126
+ if bounds .Dx () > bounds .Dy () {
127
+ // width > height
128
+ size := bounds .Dy ()
129
+ rect = image .Rect ((bounds .Dx ()- size )/ 2 , 0 , (bounds .Dx ()+ size )/ 2 , size )
130
+ } else {
131
+ // width < height
132
+ size := bounds .Dx ()
133
+ rect = image .Rect (0 , (bounds .Dy ()- size )/ 2 , size , (bounds .Dy ()+ size )/ 2 )
134
+ }
135
+
136
+ dst := image .NewRGBA (rect )
137
+ draw .Draw (dst , rect , src , rect .Min , draw .Src )
138
+ return dst
139
+ }
0 commit comments