Skip to content

Commit ec227d6

Browse files
authored
Remove nfnt/resize and oliamb/cutter (#25999)
The package `github.com/nfnt/resize` is deprecated and archived by the author. `github.com/oliamb/cutter` is not maintained since 2018. We could use `golang.org/x/image/draw` instead.
1 parent 0ef2919 commit ec227d6

File tree

4 files changed

+36
-39
lines changed

4 files changed

+36
-39
lines changed

assets/go-licenses.json

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ require (
8181
github.com/minio/sha256-simd v1.0.1
8282
github.com/msteinert/pam v1.1.0
8383
github.com/nektos/act v0.2.48
84-
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
8584
github.com/niklasfasching/go-org v1.7.0
86-
github.com/oliamb/cutter v0.2.2
8785
github.com/olivere/elastic/v7 v7.0.32
8886
github.com/opencontainers/go-digest v1.0.0
8987
github.com/opencontainers/image-spec v1.1.0-rc4

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -921,8 +921,6 @@ github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzE
921921
github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
922922
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
923923
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
924-
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
925-
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
926924
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
927925
github.com/niklasfasching/go-org v1.7.0 h1:vyMdcMWWTe/XmANk19F4k8XGBYg0GQ/gJGMimOjGMek=
928926
github.com/niklasfasching/go-org v1.7.0/go.mod h1:WuVm4d45oePiE0eX25GqTDQIt/qPW1T9DGkRscqLW5o=
@@ -939,8 +937,6 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn
939937
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
940938
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
941939
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
942-
github.com/oliamb/cutter v0.2.2 h1:Lfwkya0HHNU1YLnGv2hTkzHfasrSMkgv4Dn+5rmlk3k=
943-
github.com/oliamb/cutter v0.2.2/go.mod h1:4BenG2/4GuRBDbVm/OPahDVqbrOemzpPiG5mi1iryBU=
944940
github.com/olivere/elastic/v7 v7.0.32 h1:R7CXvbu8Eq+WlsLgxmKVKPox0oOwAE/2T9Si5BnvK6E=
945941
github.com/olivere/elastic/v7 v7.0.32/go.mod h1:c7PVmLe3Fxq77PIfY/bZmxY/TAamBhCzZ8xDOE09a9k=
946942
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=

modules/avatar/avatar.go

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import (
1717
"code.gitea.io/gitea/modules/avatar/identicon"
1818
"code.gitea.io/gitea/modules/setting"
1919

20-
"github.com/nfnt/resize"
21-
"github.com/oliamb/cutter"
20+
"golang.org/x/image/draw"
2221

2322
_ "golang.org/x/image/webp" // for processing webp images
2423
)
@@ -81,28 +80,10 @@ func processAvatarImage(data []byte, maxOriginSize int64) ([]byte, error) {
8180
}
8281

8382
// 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)
10384

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)
10687

10788
// try to encode the cropped/resized image to png
10889
bs := bytes.Buffer{}
@@ -124,3 +105,35 @@ func processAvatarImage(data []byte, maxOriginSize int64) ([]byte, error) {
124105
func ProcessAvatarImage(data []byte) ([]byte, error) {
125106
return processAvatarImage(data, setting.Avatar.MaxOriginSize)
126107
}
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

Comments
 (0)