diff options
author | harryzcy <harry@harryzheng.com> | 2023-07-20 06:52:42 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-20 19:52:42 +0800 |
commit | ec227d6682844cc3133eb527e1c877492eeda5d8 (patch) | |
tree | 6c4e4f7acb56560b7608b56e1a434e1fcb2ea5fc /modules | |
parent | 0ef2919bb6f992582cefdc4fc789f4b1e9433121 (diff) | |
download | gitea-ec227d6682844cc3133eb527e1c877492eeda5d8.tar.gz gitea-ec227d6682844cc3133eb527e1c877492eeda5d8.zip |
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.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/avatar/avatar.go | 59 |
1 files changed, 36 insertions, 23 deletions
diff --git a/modules/avatar/avatar.go b/modules/avatar/avatar.go index 10de85b74e..106215ec0b 100644 --- a/modules/avatar/avatar.go +++ b/modules/avatar/avatar.go @@ -17,8 +17,7 @@ import ( "code.gitea.io/gitea/modules/avatar/identicon" "code.gitea.io/gitea/modules/setting" - "github.com/nfnt/resize" - "github.com/oliamb/cutter" + "golang.org/x/image/draw" _ "golang.org/x/image/webp" // for processing webp images ) @@ -81,28 +80,10 @@ func processAvatarImage(data []byte, maxOriginSize int64) ([]byte, error) { } // try to crop and resize the origin image if necessary - if imgCfg.Width != imgCfg.Height { - var newSize, ax, ay int - if imgCfg.Width > imgCfg.Height { - newSize = imgCfg.Height - ax = (imgCfg.Width - imgCfg.Height) / 2 - } else { - newSize = imgCfg.Width - ay = (imgCfg.Height - imgCfg.Width) / 2 - } - - img, err = cutter.Crop(img, cutter.Config{ - Width: newSize, - Height: newSize, - Anchor: image.Point{X: ax, Y: ay}, - }) - if err != nil { - return nil, err - } - } + img = cropSquare(img) - targetSize := uint(DefaultAvatarSize * setting.Avatar.RenderedSizeFactor) - img = resize.Resize(targetSize, targetSize, img, resize.Bilinear) + targetSize := DefaultAvatarSize * setting.Avatar.RenderedSizeFactor + img = scale(img, targetSize, targetSize, draw.BiLinear) // try to encode the cropped/resized image to png bs := bytes.Buffer{} @@ -124,3 +105,35 @@ func processAvatarImage(data []byte, maxOriginSize int64) ([]byte, error) { func ProcessAvatarImage(data []byte) ([]byte, error) { return processAvatarImage(data, setting.Avatar.MaxOriginSize) } + +// scale resizes the image to width x height using the given scaler. +func scale(src image.Image, width, height int, scale draw.Scaler) image.Image { + rect := image.Rect(0, 0, width, height) + dst := image.NewRGBA(rect) + scale.Scale(dst, rect, src, src.Bounds(), draw.Over, nil) + return dst +} + +// cropSquare crops the largest square image from the center of the image. +// If the image is already square, it is returned unchanged. +func cropSquare(src image.Image) image.Image { + bounds := src.Bounds() + if bounds.Dx() == bounds.Dy() { + return src + } + + var rect image.Rectangle + if bounds.Dx() > bounds.Dy() { + // width > height + size := bounds.Dy() + rect = image.Rect((bounds.Dx()-size)/2, 0, (bounds.Dx()+size)/2, size) + } else { + // width < height + size := bounds.Dx() + rect = image.Rect(0, (bounds.Dy()-size)/2, size, (bounds.Dy()+size)/2) + } + + dst := image.NewRGBA(rect) + draw.Draw(dst, rect, src, rect.Min, draw.Src) + return dst +} |