dqnhfbc3738 2016-05-30 15:27
浏览 46
已采纳

去图片上传

I am currently uploading an image to my server doing the following:

func (base *GuildController) GuildLogo(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
    ...
    logo, _, err := req.FormFile("logo")
    defer logo.Close()
    logoGif, format, err := image.Decode(logo)
    if err != nil {
        base.Error = "Error while decoding your guild logo"
        return
    }
    logoImage, err := os.Create(pigo.Config.String("template")+"/public/guilds/"+ps.ByName("name")+".gif")
    if err != nil {
        base.Error = "Error while trying to open guild logo image"
        return
    }
    defer logoImage.Close()
    //resizedLogo := resize.Resize(64, 64, logoGif, resize.Lanczos3)
    err = gif.Encode(logoImage, logoGif, &gif.Options{
        256,
        nil,
        nil,
    })
    if err != nil {
        base.Error = "Error while encoding your guild logo"
        return
    }
    ...
}

So everything is working good. But gifs lose the animation.

For example here is a gif I want to upload

enter image description here

And here is the saved one

enter image description here

Not sure what I am doing wrong

  • 写回答

1条回答 默认 最新

  • dongliang7545 2016-05-31 19:00
    关注

    As hinted in the comments, you are just working with one frame:

    func Decode(r io.Reader) (image.Image, error) Decode reads a GIF image from r and returns the first embedded image as an image.Image.

    But you need

    func DecodeAll(r io.Reader) (*GIF, error) DecodeAll reads a GIF image from r and returns the sequential frames and timing information.

    and

    func EncodeAll(w io.Writer, g *GIF) error EncodeAll writes the images in g to w in GIF format with the given loop count and delay between frames.

    Look at this post for details.

    Here's an example that slows down the image to 0.5s each frame:

    package main
    
    import (
        "image/gif"
        "os"
    )
    
    func main() {
        logo, err := os.Open("yay.gif")
        if err != nil {
            panic(err)
        }
        defer logo.Close()
    
        inGif, err := gif.DecodeAll(logo)
        if err != nil {
            panic(err)
        }
    
        outGif, err := os.Create("done.gif")
        if err != nil {
            panic(err)
        }
        defer outGif.Close()
    
        for i := range inGif.Delay {
            inGif.Delay[i] = 50
        }
        if err := gif.EncodeAll(outGif, inGif); err != nil {
            panic(err)
        }
    }
    

    Results:

    Original Image Slowed-down version

    Side note

    Even if in my browser (Firefox) I see the output image animated, and I can see the the frames in The GIMP, I cannot see it animated on my desktop viewers (gifview, comix). I do not know (yet) what is the cause of this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 openwrt双栈NAT
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用