英文:
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)
}
对于png
和jpeg
格式的图像,它可以正常工作,但是动画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, ";")[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)
}
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论