disintegration / imaging

Imaging is a simple image processing package for Go
MIT License
5.22k stars 433 forks source link

How to get file from postman #150

Open surapong-tho opened 2 years ago

surapong-tho commented 2 years ago

I'm newbie a developer How to get image from form data type file from api(postman)

surapong-tho commented 2 years ago
log := logger.WithPrefixFunctionName()
file, err := c.FormFile("file")
if err != nil {
    log.CErrorln(c,"Get file error, ", err)
    return err
}
log.Println(file)
//image, err := file.Open()
//if err != nil {
//  log.CErrorln(c, "Open file error, ", err)
//  return err
//}
src, err := imaging.Open(file.Filename)
if err != nil {
    log.CErrorln(c, "Open file error, ", err)
    return err
}

src = imaging.Resize(src, 200, 0, imaging.Lanczos)

// Save the resulting image as JPEG.
err = imaging.Save(src, "testdata/out_example.jpg")
if err != nil {
    log.CErrorln(c, "failed to save image: %v", err)
    return err
}
return nil

this is my code thx

disintegration commented 2 years ago

Hi,

What type is c in your example?

file, err := c.FormFile("file")

The FormFile method of http.Request from standard library returns a multipart.File (https://pkg.go.dev/net/http#Request.FormFile). You can pass it to imaging.Decode to decode the image.