如何将基础64字符串转换为GIF

我在将base 64字符串转换为gif时遇到问题,我尝试了以下内容。


unbased, err := base64.StdEncoding.DecodeString(bs64)

    if err != nil {

        panic("Cannot decode b64")

    }

    var imgCoupon image.Image

    imgCoupon, err = gif.Decode(bytes.NewReader(unbased))


    var opt gif.Options

    opt.NumColors = 256

    var buff bytes.Buffer

    gif.Encode(&buff, imgCoupon, &opt)

但是当我将其上传到GCP / 谷歌云存储中时,GIF不会动画。


这就是我上传的方式。


sw := storageClient.Bucket(bucket).Object("test"+"_file"+".gif").NewWriter(ctx)


if _, err := io.Copy(sw, &buff); err != nil {

    c.JSON(http.StatusInternalServerError, gin.H{

        "message": err.Error(),

        "error":   true,

    })

    return

}

结果:

http://img.mukewang.com/631f224600013cbd03040226.jpg

它应该如何:

http://img.mukewang.com/631f225a0001917203070230.jpg

UYOU
浏览 100回答 1
1回答

慕的地10843

问题在于它是如何被解码的。 返回一个 ,但 gif。解码全部返回一个类型。gif.Decodeimage.Imagegif.GIF同样,用于写入多个帧。gif.EncodeAll这是适合我的代码:unbased, err := base64.StdEncoding.DecodeString(bs64)if err != nil {    panic("Cannot decode b64")}// Use DecodeAll to load the data into a gif.GIFimgCoupon, err := gif.DecodeAll(bytes.NewReader(unbased))imgCoupon.LoopCount = 0// EncodeAll to the buffervar buff bytes.Buffergif.EncodeAll(&buff, imgCoupon)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go