You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (go env)?
go env Output
set GO111MODULE=
set GOARCH=amd64
set GOBIN=L:\Go\apps
set GOCACHE=C:\Users\Trankten\AppData\Local\go-build
set GOENV=C:\Users\Trankten\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Trankten\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Trankten\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=L:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=L:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.6
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=L:\Go\APP\color\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\TMP\go-build1644488097=/tmp/go-build -gno-record-gcc-switches
What did you do?
package main
import (
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
)
func main() {
fh, err := os.Open("file.png")
if err != nil {
fmt.Println("ERR 1: " + err.Error())
os.Exit(1)
}
_, format, err := image.DecodeConfig(fh)
if (format != "jpeg" && format != "gif" && format != "png") || err != nil {
fmt.Println("ERR 2: " + err.Error())
os.Exit(1)
}
if format == "png" {
_, err = png.Decode(fh)
} else if format == "jpg" {
_, err = jpeg.Decode(fh)
} else if format == "gif" {
_, err = gif.Decode(fh)
} else {
fmt.Println("ERR 3: " + err.Error())
os.Exit(1)
}
if err != nil {
fmt.Println("ERR 4: " + err.Error())
os.Exit(1)
}
fmt.Println("Read OK.")
os.Exit(1)
}
What did you expect to see?
"Read OK"
What did you see instead?
ERR 4: png: invalid format: not a PNG file
The text was updated successfully, but these errors were encountered:
trankten
changed the title
affected/package:
affected/package: image/png
Feb 3, 2022
trankten
changed the title
affected/package: image/png
image/png: invalid format: not a PNG file
Feb 3, 2022
@trankten i'm facing the similar problem. I had tried the solution given by you, but unfortunately fix hadn't worked for me.
The only difference in my case is i'm taking file from request and not opening from os.
Here's the code
file, err := header.Open() /* get file from header */
if err != nil {
panic("error from opening the file : ", err)
}
file.Seek(0, 0)
config, format, err := image.DecodeConfig(file) /* decode image config */
if err != nil {
panic("error in getting config : ", err)
}
var imageDecoded image.Image
if format == "png" {
imageDecoded, err = png.Decode(file) /* decode png image */
} else {
imageDecoded, err = jpeg.Decode(file) /* decode jpg/jpeg image */
}
if err != nil {
panic("error in decoding the image : ", err)
}
Can you please help to guide me what i'm doing wrong?
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
"Read OK"
What did you see instead?
ERR 4: png: invalid format: not a PNG file
The text was updated successfully, but these errors were encountered: