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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂