From 9491b488485ac5d47626e6bdb39c5ab54d719490 Mon Sep 17 00:00:00 2001 From: jhc Date: Mon, 24 Feb 2025 18:09:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/logic/upload.go | 42 ++++++++++++++++++++++++++++++++++++ pkg/model/common.go | 19 ++++++++-------- pkg/service/upload/upload.go | 10 +++++++++ 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/pkg/logic/upload.go b/pkg/logic/upload.go index f273b61..df34740 100644 --- a/pkg/logic/upload.go +++ b/pkg/logic/upload.go @@ -48,6 +48,19 @@ func ReadOrientation(filename string) (direct int, err error) { return } +// 旋转90度 +func Rotate90(m image.Image) image.Image { + rotate90 := image.NewRGBA(image.Rect(0, 0, m.Bounds().Dy(), m.Bounds().Dx())) + // 矩阵旋转 + for x := m.Bounds().Min.Y; x < m.Bounds().Max.Y; x++ { + for y := m.Bounds().Max.X - 1; y >= m.Bounds().Min.X; y-- { + // 设置像素点 + rotate90.Set(m.Bounds().Max.Y-x, y, m.At(y, x)) + } + } + return rotate90 +} + // 旋转90度 func rotate90(m image.Image) image.Image { rotate90 := image.NewRGBA(image.Rect(0, 0, m.Bounds().Dy(), m.Bounds().Dx())) @@ -158,3 +171,32 @@ func (u *Upload) PutBos(filePath string, mediaType string, needRemove bool) (url url = fmt.Sprintf("%s/%s", config.ConfigData.Oss.CdnHost, objectName) return } + +func MakeThumbnailDefault90(imagePath, savePath string) error { + prefix := strings.ToLower(path.Ext("./2.jpg")) + + file, _ := os.Open(imagePath) + defer file.Close() + img, _, err := image.Decode(file) + if err != nil { + return err + } + img = rotate90(img) + newImg, _ := os.Create(savePath) + defer newImg.Close() + switch prefix { + case ".jpg", ".jpeg": + err = jpeg.Encode(newImg, img, &jpeg.Options{Quality: 100}) + case "png": + err = png.Encode(newImg, img) + case "bmp": + err = bmp.Encode(newImg, img) + default: + err = jpeg.Encode(newImg, img, &jpeg.Options{Quality: 100}) + } + if err != nil { + logger.Error("Encode err", err) + return err + } + return nil +} diff --git a/pkg/model/common.go b/pkg/model/common.go index 54efde5..effcc90 100644 --- a/pkg/model/common.go +++ b/pkg/model/common.go @@ -1,15 +1,16 @@ package model const ( - HttpType = "http://" - HttpsType = "https://" - TmpArtworkDir = "./runtime/tmp/artworks" - TmpArtistDir = "./runtime/tmp/artists" - MediaPath = "./runtime/" - TplPath = "./data/" - ImgActionRotate = "rotate" //旋转 - MediaTypeVideo = "video" - MediaTypeImage = "video" + HttpType = "http://" + HttpsType = "https://" + TmpArtworkDir = "./runtime/tmp/artworks" + TmpArtistDir = "./runtime/tmp/artists" + MediaPath = "./runtime/" + TplPath = "./data/" + ImgActionRotate = "rotate" //旋转 + ImgActionRotate90 = "rotate90" //旋转 + MediaTypeVideo = "video" + MediaTypeImage = "video" ) const ( diff --git a/pkg/service/upload/upload.go b/pkg/service/upload/upload.go index cc0e2d8..68ad253 100644 --- a/pkg/service/upload/upload.go +++ b/pkg/service/upload/upload.go @@ -135,6 +135,16 @@ func UploadImg(c *gin.Context) { _ = os.Remove(dst) dst = newDst } + } else if action == model.ImgActionRotate90 { + fileFullName = fmt.Sprintf("%s%s", filename, fileExt) + newDst := fmt.Sprintf("%s/%s_rotate%v", imgPath, filename, fileExt) + if err = logic.MakeThumbnailDefault90(dst, newDst); err != nil { + //ResponseQuickMsg(c, e.Failed, e.GetMsg(e.ERROR_ROTATE_IMG), nil) + //return + } else { + _ = os.Remove(dst) + dst = newDst + } } //localUrl := fmt.Sprintf("%s/%s/%s/%s/%s", config.ServerDM, BaseRoute, source, mask, fileFullName) var data map[string]string = make(map[string]string, 2)