I have successfully implemented downloading images on my web app. They show up fine in the browser; however, when I attempt to decode them for resizing, I get errors. There is a post on GitHub about this issue:
https://github.com/golang/go/issues/45902
It is not resolved there. For me, it seems to occur more frequently with larger images. These are images which work fine in most applications; for example: Affinity photo and Mac Photos. I edited one of the files in Affinity and reduced the size. After I got it down to a width of 800, I was able to decode and resize it. Originally, it was 4000px wide and exported from Mac Photos. It fails to decode with error: unexpected EOF
. The photo was taken with an iPhone 12. I see this error on most photos. After working with quite a few photos, I can consistently decode them only after I get the width down to around 800px. In every case of decode failing, the image shows fine in the browser.
I tried downloading this image:
https://hiconsumption.com/fastest-cars-in-the-world/
which is 1000px wide and get the same error on decode.
I would think this issue would be widely reported unless I am doing something wrong in my code.
var decodedImage image.Image
var err error
if fileType == "jpg" {
buf := bytes.NewBufferString(imageData)
reader := base64.NewDecoder(base64.StdEncoding, buf)
config, err := jpeg.DecodeConfig(reader)
if err != nil {
log.Println("error jpeg.DecodeConfig: ", err)
return err
} else {
log.Println("Width:", config.Width, "Height:", config.Height)
}
buf = bytes.NewBufferString(imageData)
reader = base64.NewDecoder(base64.StdEncoding, buf)
decodedImage, err = jpeg.Decode(reader)
if err != nil {
log.Println("jpeg.Decode failed error: ", err)
return err
}
}