gpt4 book ai didi

image - 将multipart.File放入image.Decode作为参数

转载 作者:行者123 更新时间:2023-12-01 21:13:43 25 4
gpt4 key购买 nike

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]]

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

最佳答案

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


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

  • 导入_“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)
    ...

    关于image - 将multipart.File放入image.Decode作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61783357/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com