将多部分文件转换为golang中的图像对象

时间:2015-10-16 11:06:25

标签: go amazon-s3

我尝试上传图片,调整图片大小,然后将其上传到Amazon S3,但是我正在努力弄清楚如何将图片从multipart.File转换为image.Image < / p>

package controllers

import (
    "github.com/gin-gonic/gin"
    "github.com/mitchellh/goamz/aws"
    "github.com/mitchellh/goamz/s3"
    "github.com/nfnt/resize"
    _ "image/jpeg"
    "log"
    "os"
    "strconv"
)

type ResizeController struct {
}

func NewResizeController() *ResizeController {
    return &ResizeController{}
}

func (rc ResizeController) Resize(c *gin.Context) {

    auth, err := aws.EnvAuth()

    if err != nil {
        log.Fatal(err)
    }

    client := s3.New(auth, aws.EUWest)
    bucket := client.Bucket(os.Getenv("AWS_BUCKET_NAME"))

    file, header, err := c.Request.FormFile("file")
    filename := header.Filename

    height := c.Query("height")
    width := c.Query("width")

    h64, err := strconv.ParseUint(height, 10, 32)
    w64, err := strconv.ParseUint(width, 10, 32)
    h := uint(h64)
    w := uint(w64)

    m := resize.Resize(w, h, file, resize.Lanczos3)

    err = bucket.Put("/content/"+filename, m, "content-type", s3.Private)

    c.JSON(200, gin.H{"filename": header.Filename})
}

我收到错误controllers/resize_controller.go:43: cannot use file (type multipart.File) as type image.Image in argument to resize.Resize:

1 个答案:

答案 0 :(得分:1)

想出来,我只需要使用

image, err := jpeg.Decode(file)