动画 GIF 未正确保存

huangapple go评论83阅读模式
英文:

Animated gif not saved correctly

问题

我使用这段代码将以base64编码的图像文件保存并从json中发送:

func SaveBase64ImageToDisk(imageString string) (interface{}, error) {
    imageExt := strings.ToLower(strings.Split(strings.Split(imageString, ";")[0], "/")[1])
    imageData := strings.Split(imageString, ";base64,")[1]
    imageReader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(imageData))

    if decodedImage, _, err := image.Decode(imageReader); err != nil {
        return nil, err
    } else {
        imageFileName, _ := _saveImage(imageExt, &decodedImage)

        return imageFileName.(string), nil
    }
}

func _saveImage(imageExt string, decodedImage *image.Image) (interface{}, error) {

    dir, err := os.Getwd()
    if err != nil {
        log.Fatal(err)
    }

    path := dir + "/media/photos/"
    imageFileName := getRandomFileName("randomstr") + "." + imageExt
    if imageFile, err := os.Create(path + imageFileName); err != nil {
        return nil, err
    } else {
        defer imageFile.Close()
        if imageExt == "png" {
            if err = png.Encode(imageFile, *decodedImage); err != nil {
                return nil, err
            }
        } else if imageExt == "jpg" {
            if err = jpeg.Encode(imageFile, *decodedImage, nil); err != nil {
                return nil, err
            }
        } else if imageExt == "gif" {
            if err = gif.Encode(imageFile, *decodedImage, nil); err != nil {
                return nil, err
            }
        }
        return imageFileName, nil
    }
}

func getRandomFileName(prefix string) string {
    rand.Seed(time.Now().UTC().UnixNano())
    l := len(prefix)
    result := make([]byte, l)
    for i := 0; i < l; i++ {
        result[i] = CHARS[rand.Intn(len(CHARS))]
    }
    return string(result)
}

对于pngjpeg格式的图像,它可以正常工作,但是动画gif图像保存为静态图像,或者在某些图像查看器中根本无法显示。我想知道如何修复这个问题?

英文:

I use this snippet to save image files encoded as base64 and sent from json:

func SaveBase64ImageToDisk(imageString string) (interface{}, error) {
imageExt := strings.ToLower(strings.Split(strings.Split(imageString, &quot;;&quot;)[0], &quot;/&quot;)[1])
imageData := strings.Split(imageString, &quot;;base64,&quot;)[1]
imageReader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(imageData))
if decodedImage, _, err := image.Decode(imageReader); err != nil {
return nil, err
} else {
imageFileName, _ := _saveImage(imageExt, &amp;decodedImage)
return imageFileName.(string), nil
}
}
func _saveImage(imageExt string, decodedImage *image.Image) (interface{}, error) {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
path := dir + &quot;/media/photos/&quot;
imageFileName := getRandomFileName(&quot;randomstr&quot;) + &quot;.&quot; + imageExt
if imageFile, err := os.Create(path + imageFileName); err != nil {
return nil, err
} else {
defer imageFile.Close()
if imageExt == &quot;png&quot; {
if err = png.Encode(imageFile, *decodedImage); err != nil {
return nil, err
}
} else if imageExt == &quot;jpg&quot; {
if err = jpeg.Encode(imageFile, *decodedImage, nil); err != nil {
return nil, err
}
} else if imageExt == &quot;gif&quot; {
if err = gif.Encode(imageFile, *decodedImage, nil); err != nil {
return nil, err
}
}
return imageFileName, nil
}
}
func getRandomFileName(prefix string) string {
rand.Seed(time.Now().UTC().UnixNano())
l := len(prefix)
result := make([]byte, l)
for i := 0; i &lt; l; i++ {
result[i] = CHARS[rand.Intn(len(CHARS))]
}
return string(result)
}

It works fine for png and jpeg but animated gif images are saved as a still image, or are not displayed at all at some image viewers. I'm wondering how can I fix this?

答案1

得分: 1

你的编码部分是正确的,但是对于解码,你应该使用image/gif包。

decodedImage, _, err := gif.Decode(imageReader)
英文:

Your Encode part is correct, however for decoding you should use image/gif package.

decodedImage, _, err := gif.Decode(imageReader)

huangapple
  • 本文由 发表于 2017年8月17日 16:53:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/45730629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定