Skip to content

Commit 3650b03

Browse files
committed
图片裁切
1 parent 62c256b commit 3650b03

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.17
55
require (
66
github.com/KenmyZhang/aliyun-communicate v0.0.0-20180308134849-7997edc57454
77
github.com/bxcodec/faker/v3 v3.7.0
8+
github.com/disintegration/imaging v1.6.2
89
github.com/gertd/go-pluralize v0.1.7
910
github.com/gin-gonic/gin v1.7.7
1011
github.com/go-redis/redis/v8 v8.11.4
@@ -61,7 +62,7 @@ require (
6162
github.com/ugorji/go/codec v1.2.6 // indirect
6263
go.uber.org/atomic v1.7.0 // indirect
6364
go.uber.org/multierr v1.6.0 // indirect
64-
golang.org/x/image v0.0.0-20190802002840-cff245a6509b // indirect
65+
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
6566
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
6667
golang.org/x/text v0.3.7 // indirect
6768
google.golang.org/protobuf v1.27.1 // indirect

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
107107
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
108108
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
109109
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
110+
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
111+
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
110112
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
111113
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
112114
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
@@ -501,8 +503,9 @@ golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EH
501503
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
502504
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
503505
golang.org/x/image v0.0.0-20190501045829-6d32002ffd75/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
504-
golang.org/x/image v0.0.0-20190802002840-cff245a6509b h1:+qEpEAPhDZ1o0x3tHzZTQDArnOixOzGD9HUJfcg0mb4=
505506
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
507+
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
508+
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
506509
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
507510
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
508511
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

pkg/file/file.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"path/filepath"
1313
"strings"
1414

15+
"github.com/disintegration/imaging"
1516
"github.com/gin-gonic/gin"
1617
)
1718

@@ -31,9 +32,11 @@ func Exists(fileToCheck string) bool {
3132
}
3233
return true
3334
}
35+
3436
func FileNameWithoutExtension(fileName string) string {
3537
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
3638
}
39+
3740
func SaveUploadAvatar(c *gin.Context, file *multipart.FileHeader) (string, error) {
3841

3942
var avatar string
@@ -50,7 +53,26 @@ func SaveUploadAvatar(c *gin.Context, file *multipart.FileHeader) (string, error
5053
return avatar, err
5154
}
5255

53-
return avatarPath, nil
56+
// 裁切图片
57+
img, err := imaging.Open(avatarPath, imaging.AutoOrientation(true))
58+
if err != nil {
59+
return avatar, err
60+
}
61+
resizeAvatar := imaging.Thumbnail(img, 256, 256, imaging.Lanczos)
62+
resizeAvatarName := randomNameFromUploadFile(file)
63+
resizeAvatarPath := publicPath + dirName + resizeAvatarName
64+
err = imaging.Save(resizeAvatar, resizeAvatarPath)
65+
if err != nil {
66+
return avatar, err
67+
}
68+
69+
// 删除老文件
70+
err = os.Remove(avatarPath)
71+
if err != nil {
72+
return avatar, err
73+
}
74+
75+
return dirName + resizeAvatarName, nil
5476
}
5577

5678
func randomNameFromUploadFile(file *multipart.FileHeader) string {

0 commit comments

Comments
 (0)