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 } ) + } }