Skip to content

Instantly share code, notes, and snippets.

@akhenakh
Last active March 13, 2024 13:18
Show Gist options
  • Save akhenakh/8462840 to your computer and use it in GitHub Desktop.
Save akhenakh/8462840 to your computer and use it in GitHub Desktop.
Guess an image format in Golang
package main
import (
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"mime"
_ "golang.org/x/image/webp"
)
// Guess image format from gif/jpeg/png/webp
func guessImageFormat(r io.Reader) (format string, err error) {
_, format, err = image.DecodeConfig(r)
return
}
// Guess image mime types from gif/jpeg/png/webp
func guessImageMimeTypes(r io.Reader) string {
format, _ := guessImageFormat(r)
if format == "" {
return ""
}
return mime.TypeByExtension("." + format)
}
@issaclin32
Copy link

issaclin32 commented Mar 20, 2020

The vp8-go package seems to be deprecated. We can use golang.org/x/image/webp instead.

@akhenakh
Copy link
Author

@issaclin32 updated thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment