将 multipart.File 作为参数放入 image.Decode
Getting a multipart.File into image.Decode as argument
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)
...
错误打印:"image: unknown format"
我还打印了 multipart.FileHeader.Header,上面写着:
map[Content-Disposition:[form-data; name="img1"; filename="img1"] Content-Type:[application/octet-stream]]
有人遇到过这个吗?任何有用的建议都会有所帮助!非常感谢
正如图像包的文档中所述 (https://golang.org/pkg/image/ 92),您必须首先注册要使用的格式:
Decoding any particular image format requires the prior registration
of a decoder function. Registration is typically automatic as a side
effect of initializing that format’s package so that, to decode a PNG
image, it suffices to have
import _ “image/png”
in a program’s main package. The _ means to import a package purely
for its initialization side effects.
实现示例: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)
...
使用以下包解决:"github.com/vincent-petithory/dataurl"
例如:
img解码,_ := dataurl.Decode(imgupload)
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)
...
错误打印:"image: unknown format"
我还打印了 multipart.FileHeader.Header,上面写着:
map[Content-Disposition:[form-data; name="img1"; filename="img1"] Content-Type:[application/octet-stream]]
有人遇到过这个吗?任何有用的建议都会有所帮助!非常感谢
正如图像包的文档中所述 (https://golang.org/pkg/image/ 92),您必须首先注册要使用的格式:
Decoding any particular image format requires the prior registration of a decoder function. Registration is typically automatic as a side effect of initializing that format’s package so that, to decode a PNG image, it suffices to have
import _ “image/png”
in a program’s main package. The _ means to import a package purely for its initialization side effects.
实现示例: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)
...
使用以下包解决:"github.com/vincent-petithory/dataurl"
例如:
img解码,_ := dataurl.Decode(imgupload)