在golang中将多部分文件转换为图像对象
我正试图上传一个图像,调整它的大小,然后在go中上传到AmazonS3,但是我很难弄清楚如何将图像从multipart.File转换为image.image在golang中将多部分文件转换为图像对象,go,amazon-s3,Go,Amazon S3,我正试图上传一个图像,调整它的大小,然后在go中上传到AmazonS3,但是我很难弄清楚如何将图像从multipart.File转换为image.image package controllers import ( "github.com/gin-gonic/gin" "github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/s3" "github.com/nfnt/resize" _ "i
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\u controller.go:43:无法使用file(键入multipart.file)作为参数中的image.image来调整大小。resize:
解决了这个问题,我只需要使用
image,err:=jpeg.Decode(文件)
请注意,这将仅对JPG图像进行解码。最好使用哪个自动检测图像格式。但请记住,要使其工作,您需要导入,例如,import.\uu“image/png”
。详见文件包。