添加参数判断是不是gif动图

master
DESKTOP-4RNDQIC\29019 2021-02-03 21:43:49 +08:00
parent 738184fa47
commit 43e0a6c865
1 changed files with 35 additions and 40 deletions

View File

@ -2,16 +2,20 @@ package controller
import ( import (
"background/utils" "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"
"image/color" "image/color"
"image/jpeg"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "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" // "github.com/nfnt/resize"
// "image/jpeg" // "image/jpeg"
"image/gif" "image/gif"
@ -27,31 +31,22 @@ func (this *FileController) OnUpload(c *gin.Context) {
log.Print(e.Error()) log.Print(e.Error())
return return
} }
imgtype := c.Query("type")
log.Print(imgtype)
file, _, err := c.Request.FormFile("image") file, _, err := c.Request.FormFile("image")
if nil != err || nil == file{ if nil != err || nil == file{
log.Print(err.Error()) log.Print(err.Error())
return 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{ if imgtype == "gif"{
// log.Print(err.Error())
// // return
// }
// }
// if name == "gif"{
allgifs,er := gif.DecodeAll(file) allgifs,er := gif.DecodeAll(file)
if nil != er{ if nil != er{
log.Print("decode error",er.Error()) log.Print("decode error",er.Error())
return return
} }
datout, err := os.Create("image/" + uid.String() + ".jpg" ) datout, err := os.Create("image/" + uid.String() + ".gif" )
defer datout.Close() defer datout.Close()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -59,29 +54,29 @@ func (this *FileController) OnUpload(c *gin.Context) {
} }
gif.EncodeAll(datout,allgifs) gif.EncodeAll(datout,allgifs)
c.JSON(200, map[string] interface{}{"url":uid.String() + ".jpg" } ) c.JSON(200, map[string] interface{}{"url":uid.String() + ".gif" } )
// }else{ }else{
// img, _, err := image.Decode(file) img, name, err := image.Decode(file)
// if nil != err{ if nil != err{
// log.Print(err.Error()) log.Print(err.Error())
// return 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 datout, err := os.Create("image/" + uid.String() + "." + name)
// // and preserve aspect ratio defer datout.Close()
// if(dx > 800){ if err != nil {
// dx = dx / 2; log.Fatal(err)
// } }
// m := resize.Resize(uint(dx), 0, img, resize.Lanczos3) 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 } )
// }
} }