I am trying to take a multipart.File that is an io.Reader and decode it as a jpeg to covert into a Thumbnail using github.com/disintegration/imaging's library. I know in advance the data is going to be a jpeg. When I send the multipart.File to a ConvertImageToThumbnail function and it returns Unexpected EOF every time. What am I doing wrong?
package images
import (
"github.com/disintegration/imaging"
"image"
"image/jpeg"
"mime/multipart"
)
func ConvertImageToThumbnail(pic multipart.File) (image.Image, error) {
pic.Seek(0,0) // The solution was to seek back to the beginning of the file
img,err := jpeg.Decode(pic)
if err != nil {
return nil, err
}
thumb := imaging.Thumbnail(img, 100, 100, imaging.CatmullRom)
return thumb, nil
}
pic, header, err := r.FormFile("avatar")
// check error
defer pic.Close()
pic.Seek(0,0) before the decode fixed the issue.