Last active
March 13, 2024 13:18
-
-
Save akhenakh/8462840 to your computer and use it in GitHub Desktop.
Guess an image format in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The vp8-go package seems to be deprecated. We can use
golang.org/x/image/webp
instead.