🐛 change Val types
This commit is contained in:
parent
c02455d509
commit
5b7b13f96a
@ -1,8 +1,12 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"catface/app/global/consts"
|
||||
"catface/app/global/variable"
|
||||
"catface/app/http/validator/core/data_transfer"
|
||||
"catface/app/service/upload_file"
|
||||
"catface/app/utils/response"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@ -14,9 +18,20 @@ func (e *Encounters) Create(context *gin.Context) {
|
||||
// TODO 处理 Photos 文件,然后处理出 Avatar,并获取压缩后的 宽高,以及文件的存储路径。
|
||||
photos := data_transfer.GetStringSlice(context, "photos")
|
||||
if len(photos) > 0 {
|
||||
userId := context.GetString(consts.ValidatorPrefix + "user_id")
|
||||
avatar := photos[0]
|
||||
avatarWidth := variable.ConfigYml.GetFloat64("FileUploadSetting.AvatarWidth")
|
||||
|
||||
srcPath := filepath.Join(variable.BasePath, "encounterPhotos", "hum_"+userId, avatar)
|
||||
dstPath := filepath.Join(variable.BasePath, "encounterAvatar", "hum_"+userId, avatar)
|
||||
avatarHeight, err := upload_file.ResizeImage(srcPath, dstPath, int(avatarWidth))
|
||||
if err != nil {
|
||||
response.Fail(context, consts.FilesUploadFailCode, consts.FilesUploadFailMsg, "")
|
||||
return
|
||||
}
|
||||
context.Set(consts.ValidatorPrefix+"avatar", avatar)
|
||||
context.Set(consts.ValidatorPrefix+"avatar_height", avatarHeight)
|
||||
context.Set(consts.ValidatorPrefix+"avatar_width", int(avatarWidth))
|
||||
}
|
||||
// Real Insert
|
||||
// if model.CreateEncounterFactory("").InsertDate(context) {
|
||||
|
@ -11,14 +11,14 @@ import (
|
||||
|
||||
type Create struct {
|
||||
UserId int `form:"user_id" json:"user_id" binding:"required,numeric"`
|
||||
AnimalsId string `form:"animals_id" json:"animals_id" binding:"required"`
|
||||
AnimalsId []int `form:"animals_id" json:"animals_id" binding:"required"`
|
||||
Title string `form:"title" json:"title" binding:"required"`
|
||||
Content string `form:"content" json:"content"`
|
||||
|
||||
// Avatar string `form:"avatar" json:"avatar"`
|
||||
Photos string `form:"photos" json:"photos"` // INFO 如果 Photo 为空,那就选取 Animals 的 Avatar
|
||||
Laitude float64 `form:"latitude" json:"latitude"`
|
||||
Longitude float64 `form:"longitude" json:"longitude"`
|
||||
Photos []string `form:"photos" json:"photos"` // INFO 如果 Photo 为空,那就选取 Animals 的 Avatar
|
||||
Laitude float64 `form:"latitude" json:"latitude"`
|
||||
Longitude float64 `form:"longitude" json:"longitude"`
|
||||
}
|
||||
|
||||
func (c Create) CheckParams(context *gin.Context) {
|
||||
|
@ -2,23 +2,31 @@ package upload_file
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/jpeg"
|
||||
// "image/jpeg"
|
||||
"os"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
// ResizeImage 按照指定宽度等比例缩放图片
|
||||
func ResizeImage(srcPath string, dstPath string, targetWidth int) error {
|
||||
/**
|
||||
* @description: ResizeImage 按照指定宽度等比例缩放图片
|
||||
* @param {string} srcPath 需要完整路径
|
||||
* @param {string} dstPath
|
||||
* @param {int} targetWidth
|
||||
* @return {*}
|
||||
*/
|
||||
func ResizeImage(srcPath string, dstPath string, targetWidth int) (targetHeight int, err error) {
|
||||
// 打开源图片文件
|
||||
srcFile, err := os.Open(srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
// 解码源图片
|
||||
srcImg, _, err := image.Decode(srcFile)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
// 获取源图片的尺寸
|
||||
@ -27,26 +35,15 @@ func ResizeImage(srcPath string, dstPath string, targetWidth int) error {
|
||||
srcHeight := bounds.Dy()
|
||||
|
||||
// 计算目标高度
|
||||
targetHeight := int(float64(srcHeight) * (float64(targetWidth) / float64(srcWidth)))
|
||||
targetHeight = int(float64(srcHeight) * (float64(targetWidth) / float64(srcWidth)))
|
||||
|
||||
// 创建目标图片
|
||||
dstImg := image.NewRGBA(image.Rect(0, 0, targetWidth, targetHeight))
|
||||
|
||||
dstImg := imaging.Thumbnail(srcImg, targetWidth, targetHeight, imaging.Lanczos)
|
||||
// image.NewRGBA(image.Rect(0, 0, targetWidth, targetHeight))
|
||||
// 使用高质量的滤波算法进行缩放
|
||||
draw.CatmullRom.Scale(dstImg, dstImg.Bounds(), srcImg, srcImg.Bounds(), draw.Over, nil)
|
||||
// draw.CatmullRom.Scale(dstImg, dstImg.Bounds(), srcImg, srcImg.Bounds(), draw.Over, nil)
|
||||
|
||||
// 打开目标图片文件
|
||||
dstFile, err := os.Create(dstPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dstFile.Close()
|
||||
|
||||
// 编码并保存目标图片
|
||||
err = jpeg.Encode(dstFile, dstImg, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
// Save
|
||||
err = imaging.Save(dstImg, dstPath)
|
||||
return
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -24,6 +24,7 @@ require (
|
||||
github.com/pilu/fresh v0.0.0-20240621171608-8d1fef547a99 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578 // indirect
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
modernc.org/libc v1.22.2 // indirect
|
||||
modernc.org/mathutil v1.5.0 // indirect
|
||||
@ -42,6 +43,7 @@ require (
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/dchest/captcha v1.0.0 // indirect
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||
github.com/disintegration/imaging v1.6.2
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
|
||||
github.com/gin-contrib/pprof v1.5.0 // indirect
|
||||
|
4
go.sum
4
go.sum
@ -37,6 +37,8 @@ github.com/dchest/captcha v1.0.0 h1:vw+bm/qMFvTgcjQlYVTuQBJkarm5R0YSsDKhm1HZI2o=
|
||||
github.com/dchest/captcha v1.0.0/go.mod h1:7zoElIawLp7GUMLcj54K9kbw+jEyvz2K0FDdRRYhvWo=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
||||
github.com/dnaeon/go-vcr v1.1.0/go.mod h1:M7tiix8f0r6mKKJ3Yq/kqU1OYf3MnfmBWVbPx/yU9ko=
|
||||
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
@ -204,6 +206,8 @@ golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
|
||||
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
|
Loading…
x
Reference in New Issue
Block a user