diff options
author | Rob Watson <rfwatson@users.noreply.github.com> | 2019-05-25 13:46:14 +0200 |
---|---|---|
committer | Lauris BH <lauris@nix.lv> | 2019-05-25 14:46:14 +0300 |
commit | df2557835b2235b48d1ed979abb1a1d42607e96a (patch) | |
tree | 31ff762fc5c0023f7ddad6f7673b9a210e5021ed /models | |
parent | 5f05aa13e00eb9f8098066c81d2cd916d91e9874 (diff) | |
download | gitea-df2557835b2235b48d1ed979abb1a1d42607e96a.tar.gz gitea-df2557835b2235b48d1ed979abb1a1d42607e96a.zip |
Improve handling of non-square avatars (#7025)
* Crop avatar before resizing (#1268)
Signed-off-by: Rob Watson <rfwatson@users.noreply.github.com>
* Fix spelling error
Signed-off-by: Rob Watson <rfwatson@users.noreply.github.com>
Diffstat (limited to 'models')
-rw-r--r-- | models/user.go | 22 |
1 files changed, 3 insertions, 19 deletions
diff --git a/models/user.go b/models/user.go index 7c7e81830e..f57c5a615d 100644 --- a/models/user.go +++ b/models/user.go @@ -6,7 +6,6 @@ package models import ( - "bytes" "container/list" "crypto/md5" "crypto/sha256" @@ -14,7 +13,6 @@ import ( "encoding/hex" "errors" "fmt" - "image" // Needed for jpeg support _ "image/jpeg" @@ -39,7 +37,6 @@ import ( "github.com/go-xorm/builder" "github.com/go-xorm/core" "github.com/go-xorm/xorm" - "github.com/nfnt/resize" "golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/ssh" ) @@ -457,24 +454,11 @@ func (u *User) IsPasswordSet() bool { // UploadAvatar saves custom avatar for user. // FIXME: split uploads to different subdirs in case we have massive users. func (u *User) UploadAvatar(data []byte) error { - imgCfg, _, err := image.DecodeConfig(bytes.NewReader(data)) + m, err := avatar.Prepare(data) if err != nil { - return fmt.Errorf("DecodeConfig: %v", err) - } - if imgCfg.Width > setting.AvatarMaxWidth { - return fmt.Errorf("Image width is to large: %d > %d", imgCfg.Width, setting.AvatarMaxWidth) - } - if imgCfg.Height > setting.AvatarMaxHeight { - return fmt.Errorf("Image height is to large: %d > %d", imgCfg.Height, setting.AvatarMaxHeight) - } - - img, _, err := image.Decode(bytes.NewReader(data)) - if err != nil { - return fmt.Errorf("Decode: %v", err) + return err } - m := resize.Resize(avatar.AvatarSize, avatar.AvatarSize, img, resize.NearestNeighbor) - sess := x.NewSession() defer sess.Close() if err = sess.Begin(); err != nil { @@ -497,7 +481,7 @@ func (u *User) UploadAvatar(data []byte) error { } defer fw.Close() - if err = png.Encode(fw, m); err != nil { + if err = png.Encode(fw, *m); err != nil { return fmt.Errorf("Encode: %v", err) } |