为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
UYOU 2022-09-12 20:12:35
我在将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}结果:它应该如何:
查看完整描述

1 回答

?
慕的地10843

TA贡献1785条经验 获得超8个赞

问题在于它是如何被解码的。 返回一个 ,但 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.GIF

imgCoupon, err := gif.DecodeAll(bytes.NewReader(unbased))

imgCoupon.LoopCount = 0


// EncodeAll to the buffer

var buff bytes.Buffer

gif.EncodeAll(&buff, imgCoupon)


查看完整回答
反对 回复 2022-09-12
  • 1 回答
  • 0 关注
  • 99 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号