Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image/png: invalid format: not a PNG file #50992

Closed
trankten opened this issue Feb 3, 2022 · 2 comments
Closed

image/png: invalid format: not a PNG file #50992

trankten opened this issue Feb 3, 2022 · 2 comments

Comments

@trankten
Copy link

trankten commented Feb 3, 2022

What version of Go are you using (go version)?

1.17.6

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

file

@trankten trankten changed the title affected/package: affected/package: image/png Feb 3, 2022
@trankten trankten changed the title affected/package: image/png image/png: invalid format: not a PNG file Feb 3, 2022
@trankten
Copy link
Author

trankten commented Feb 3, 2022

Fixed. Forgot to Seek back to 0,0

fh.Seek(0, 0)

@trankten trankten closed this as completed Feb 3, 2022
@nidhi-canopas
Copy link

nidhi-canopas commented Apr 19, 2023

@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?

@golang golang locked and limited conversation to collaborators Apr 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants