From 43e0a6c8653f3ea4b08fe00414b87b18cd5cce6e Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Wed, 3 Feb 2021 21:43:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=82=E6=95=B0=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=98=AF=E4=B8=8D=E6=98=AFgif=E5=8A=A8=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/file.go | 75 ++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/controller/file.go b/controller/file.go index 814fff0..c6a5abf 100644 --- a/controller/file.go +++ b/controller/file.go @@ -2,16 +2,20 @@ package controller import ( "background/utils" - "github.com/disintegration/imaging" - "github.com/gin-gonic/gin" - json "github.com/json-iterator/go" - uuid "github.com/satori/go.uuid" "image" "image/color" + "image/jpeg" "io/ioutil" "log" "net/http" "os" + + "github.com/disintegration/imaging" + "github.com/gin-gonic/gin" + json "github.com/json-iterator/go" + "github.com/nfnt/resize" + uuid "github.com/satori/go.uuid" + // "github.com/nfnt/resize" // "image/jpeg" "image/gif" @@ -27,31 +31,22 @@ func (this *FileController) OnUpload(c *gin.Context) { log.Print(e.Error()) return } - + imgtype := c.Query("type") + log.Print(imgtype) file, _, err := c.Request.FormFile("image") if nil != err || nil == file{ log.Print(err.Error()) return } - // config, name, err := image.DecodeConfig(file) - // dx := config.Width - // log.Print(config.Width,name) - // if err != nil { - // if strings.Contains(err.Error(),"gif") { - - // }else{ - // log.Print(err.Error()) - // // return - // } - // } - // if name == "gif"{ + + if imgtype == "gif"{ allgifs,er := gif.DecodeAll(file) if nil != er{ log.Print("decode error",er.Error()) return } - datout, err := os.Create("image/" + uid.String() + ".jpg" ) + datout, err := os.Create("image/" + uid.String() + ".gif" ) defer datout.Close() if err != nil { log.Fatal(err) @@ -59,29 +54,29 @@ func (this *FileController) OnUpload(c *gin.Context) { } gif.EncodeAll(datout,allgifs) - c.JSON(200, map[string] interface{}{"url":uid.String() + ".jpg" } ) - // }else{ - // img, _, err := image.Decode(file) - // if nil != err{ - // log.Print(err.Error()) - // return - // } + c.JSON(200, map[string] interface{}{"url":uid.String() + ".gif" } ) + }else{ + img, name, err := image.Decode(file) + if nil != err{ + log.Print(err.Error()) + return + } + dx := img.Bounds().Dx() + // resize to width 1000 using Lanczos resampling + // and preserve aspect ratio + if(dx > 800){ + dx = dx / 2; + } + m := resize.Resize(uint(dx), 0, img, resize.Lanczos3) - // // resize to width 1000 using Lanczos resampling - // // and preserve aspect ratio - // if(dx > 800){ - // dx = dx / 2; - // } - // m := resize.Resize(uint(dx), 0, img, resize.Lanczos3) - - // datout, err := os.Create("image/" + uid.String() + "." + name) - // defer datout.Close() - // if err != nil { - // log.Fatal(err) - // } - // jpeg.Encode(datout, m, nil) - // c.JSON(200, map[string] interface{}{"url":uid.String() + "." + name } ) - // } + datout, err := os.Create("image/" + uid.String() + "." + name) + defer datout.Close() + if err != nil { + log.Fatal(err) + } + jpeg.Encode(datout, m, nil) + c.JSON(200, map[string] interface{}{"url":uid.String() + "." + name } ) + } }