-
-
Save tsdtsdtsd/23e12a740935a4b22dedbc4f4238502c 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" | |
_ "code.google.com/p/vp8-go/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