将multipart.File放入图像。解码为参数

问题描述 投票:0回答:1

multipart.File的内容如下:“ data:image / jpeg; base64,/ 9j / 4AAQSkZJRgABAQAAAQABAAD ...”

我正在尝试将其放入Image.DecodeConfig()方法中,如下所示:

import (
    "image"
    "image/jpeg"
    "io"
)

func ImgCheckSize(file io.Reader) (io.Reader, error) {
    config, format, err := image.DecodeConfig(file)
...

错误打印:“图像:未知格式”

我还打印了multipart.FileHeader.Header,内容为:

map[Content-Disposition:[form-data; name="img1"; filename="img1"] Content-Type:[application/octet-stream]]

以前有人遇到过吗?任何有用的建议都会有所帮助!非常感谢

image go multipart
1个回答
0
投票

如图像包文档中的说明(https://golang.org/pkg/image/ 92中所述,您必须先注册要使用的格式:

  • 解码任何特定的图像格式需要事先注册解码器功能。注册通常是自动进行的初始化该格式的程序包的效果,以便解码PNG图片,只要具有

  • import _“ image / png”

  • 在程序的主程序包中。 _表示纯粹导入软件包初始化副作用。

实现示例:https://play.golang.org/p/7d1gS7_tdc0

import (
    "image"
    // Package image/jpeg is not used explicitly in the code below,
    // but is imported for its initialization side-effect, which allows
    // image.Decode to understand JPEG formatted images. 
    _ "image/jpeg"
    "io"
)

func ImgCheckSize(file io.Reader) (io.Reader, error) {
    config, format, err := image.DecodeConfig(file)
...
© www.soinside.com 2019 - 2024. All rights reserved.