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