Skip to content

Commit

Permalink
修正 上传图片 小于 设置的上传图片尺寸限制 时出现的等比放大问题
Browse files Browse the repository at this point in the history
  • Loading branch information
haierkeys committed Dec 2, 2024
1 parent a6f3fc0 commit 3252707
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/go-release-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ jobs:
- name: Append latest if master branches # env '${IMAGE_TAG},latest'
#eif: env.IMAGE_TAG == env.TAG_VERSION
run: echo IMAGE_TAG=${IMAGE_TAG},master,latest >> ${GITHUB_ENV}
- name: Environment Printer
uses: managedkaos/print-env@v1.0

- uses: docker/metadata-action@v4
id: meta
Expand Down
83 changes: 44 additions & 39 deletions internal/service/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ func (svc *Service) fileSyncHandle(fileType upload.FileType, file multipart.File
fileKey := upload.GetSavePreDirPath() + fileName

var up = make(map[string]Uploader)

var dstFileKey string

writer := &bytes.Buffer{}

// 压缩
_, err := file.Seek(0, 0)

Expand All @@ -79,47 +80,51 @@ func (svc *Service) fileSyncHandle(fileType upload.FileType, file multipart.File

size := img.Bounds().Size()

wRatio := float64(size.X) / float64(global.Config.App.ImageMaxSizeWidth)
hRatio := float64(size.Y) / float64(global.Config.App.ImageMaxSizeHeight)
ratio := wRatio
if hRatio > wRatio {
ratio = hRatio
}
// 创建新的图像对象
newWidth := uint(float64(size.X) / ratio)
newHeight := uint(float64(size.Y) / ratio)
if size.X > global.Config.App.ImageMaxSizeWidth || size.Y > global.Config.App.ImageMaxSizeHeight {

writer := &bytes.Buffer{}
wRatio := float64(size.X) / float64(global.Config.App.ImageMaxSizeWidth)
hRatio := float64(size.Y) / float64(global.Config.App.ImageMaxSizeHeight)
ratio := wRatio
if hRatio > wRatio {
ratio = hRatio
}
// 创建新的图像对象
newWidth := uint(float64(size.X) / ratio)
newHeight := uint(float64(size.Y) / ratio)

// 调整图片大小
newImage := resize.Resize(newWidth, newHeight, img, resize.Lanczos3)

switch filetype {
case "png":
err = png.Encode(writer, newImage)
case "gif":
err = gif.Encode(writer, newImage, &gif.Options{NumColors: 256})
case "jpeg", "jpg":
err = jpeg.Encode(writer, newImage, &jpeg.Options{Quality: global.Config.App.ImageQuality})
case "bmp":
err = bmp.Encode(writer, newImage)
case "tif", "tiff":
err = tiff.Encode(writer, newImage, nil)
case "webp":
cType = "image/jpg"
ext := upload.GetFileExt(fileKey)
fileKey = fileKey[0:len(fileKey)-len(ext)] + ".jpg"

err = jpeg.Encode(writer, newImage, &jpeg.Options{Quality: global.Config.App.ImageQuality})
case "avif":
err = avif.Encode(writer, newImage, avif.Options{Quality: global.Config.App.ImageQuality})

default:
return nil, errors.New("Unknown image type:" + filetype)
}

// 调整图片大小
newImage := resize.Resize(newWidth, newHeight, img, resize.Lanczos3)

switch filetype {
case "png":
err = png.Encode(writer, newImage)
case "gif":
err = gif.Encode(writer, newImage, &gif.Options{NumColors: 256})
case "jpeg", "jpg":
err = jpeg.Encode(writer, newImage, &jpeg.Options{Quality: global.Config.App.ImageQuality})
case "bmp":
err = bmp.Encode(writer, newImage)
case "tif", "tiff":
err = tiff.Encode(writer, newImage, nil)
case "webp":
cType = "image/jpg"
ext := upload.GetFileExt(fileKey)
fileKey = fileKey[0:len(fileKey)-len(ext)] + ".jpg"

err = jpeg.Encode(writer, newImage, &jpeg.Options{Quality: global.Config.App.ImageQuality})
case "avif":
err = avif.Encode(writer, newImage, avif.Options{Quality: global.Config.App.ImageQuality})

default:
return nil, errors.New("Unknown image type:" + filetype)
}
if err != nil {
return nil, err
}

if err != nil {
return nil, err
} else {
_, err = io.Copy(writer, file)
}

for _, v := range []string{"local_fs", "oss", "cloudflare_r2", "aws_s3"} {
Expand Down

0 comments on commit 3252707

Please sign in to comment.