测试动图gif成功

master
DESKTOP-4RNDQIC\29019 2021-02-03 01:47:01 +08:00
parent 97962d0145
commit 738184fa47
2 changed files with 56 additions and 27 deletions

View File

@ -50,18 +50,18 @@ func GetArticles(c *gin.Context) {
if req.Type == 110 { if req.Type == 110 {
is_public := 0 is_public := 0
if public == "true" { if public == "true" {
is_public = 0
} else {
is_public = 1 is_public = 1
} else {
is_public = 0
} }
sql = "select * from doc where doc.title like '%%%s%%' and is_public = %d limit %d offset %d " sql = "select * from doc where doc.title like '%%%s%%' and is_public = %d limit %d offset %d "
sql = fmt.Sprintf(sql, req.Name, is_public, limit, offset*limit) sql = fmt.Sprintf(sql, req.Name, is_public, limit, offset*limit)
} else { } else {
is_public := 0 is_public := 0
if public == "true" { if public == "true" {
is_public = 0
} else {
is_public = 1 is_public = 1
} else {
is_public = 0
} }
sql = fmt.Sprintf("select * from doc where doc.title like '%%%s%%' and doc.type = %d and is_public = %d limit %d offset %d ", sql = fmt.Sprintf("select * from doc where doc.title like '%%%s%%' and doc.type = %d and is_public = %d limit %d offset %d ",
req.Name, req.Type, is_public, limit, offset*limit) req.Name, req.Type, is_public, limit, offset*limit)

View File

@ -12,8 +12,10 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"github.com/nfnt/resize" // "github.com/nfnt/resize"
"image/jpeg" // "image/jpeg"
"image/gif"
// "strings"
) )
type FileController struct { type FileController struct {
@ -31,28 +33,55 @@ func (this *FileController) OnUpload(c *gin.Context) {
log.Print(err.Error()) log.Print(err.Error())
return return
} }
img, name, err := image.Decode(file) // config, name, err := image.DecodeConfig(file)
bound := img.Bounds() // dx := config.Width
dx := bound.Dx() // log.Print(config.Width,name)
dy := bound.Dy() // if err != nil {
log.Printf("%d %d %s",dx,dy,name) // if strings.Contains(err.Error(),"gif") {
if err != nil {
log.Print(err)
}
// 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) // }else{
// log.Print(err.Error())
// // return
// }
// }
// if name == "gif"{
allgifs,er := gif.DecodeAll(file)
if nil != er{
log.Print("decode error",er.Error())
return
}
datout, err := os.Create("image/" + uid.String() + ".jpg" )
defer datout.Close() defer datout.Close()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return
} }
jpeg.Encode(datout, m, nil)
c.JSON(200, map[string] interface{}{"url":uid.String() + "." + name } ) 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
// }
// // 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 } )
// }
} }