update avatar img size

This commit is contained in:
aichy126 2022-10-28 11:15:46 +08:00
parent 09f1e8dde7
commit 1563c6b583
2 changed files with 10 additions and 9 deletions

View File

@ -32,10 +32,8 @@ func (am *AvatarMiddleware) AvatarThumb() gin.HandlerFunc {
return func(ctx *gin.Context) {
u := ctx.Request.RequestURI
if strings.Contains(u, "/uploads/avatar/") {
wstr := ctx.Query("width")
hstr := ctx.Query("height")
w := converter.StringToInt(wstr)
h := converter.StringToInt(hstr)
sizeStr := ctx.Query("s")
size := converter.StringToInt(sizeStr)
uUrl, err := url.Parse(u)
if err != nil {
ctx.Next()
@ -45,10 +43,10 @@ func (am *AvatarMiddleware) AvatarThumb() gin.HandlerFunc {
uploadPath := am.serviceConfig.UploadPath
filePath := fmt.Sprintf("%s/avatar/%s", uploadPath, urlfileName)
var avatarfile []byte
if w == 0 && h == 0 {
if size == 0 {
avatarfile, err = ioutil.ReadFile(filePath)
} else {
avatarfile, err = am.uploaderService.AvatarThumbFile(ctx, uploadPath, urlfileName, w, h)
avatarfile, err = am.uploaderService.AvatarThumbFile(ctx, uploadPath, urlfileName, size)
}
if err != nil {
ctx.Next()

View File

@ -62,9 +62,12 @@ var FormatExts = map[string]imaging.Format{
".bmp": imaging.BMP,
}
func (us *UploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileName string, w, h int) (
func (us *UploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileName string, size int) (
avatarfile []byte, err error) {
thumbFileName := fmt.Sprintf("%d_%d@%s", w, h, fileName)
if size > 1024 {
size = 1024
}
thumbFileName := fmt.Sprintf("%d_%d@%s", size, size, fileName)
thumbfilePath := fmt.Sprintf("%s/%s/%s", uploadPath, avatarThumbSubPath, thumbFileName)
avatarfile, err = ioutil.ReadFile(thumbfilePath)
if err == nil {
@ -80,7 +83,7 @@ func (us *UploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileNam
if err != nil {
return avatarfile, errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
}
new_image := imaging.Fill(img, w, h, imaging.Center, imaging.Linear)
new_image := imaging.Fill(img, size, size, imaging.Center, imaging.Linear)
var buf bytes.Buffer
fileSuffix := path.Ext(fileName)