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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)