Skip to content

Instantly share code, notes, and snippets.

@tsdtsdtsd
Forked from akhenakh/guess.go
Created July 26, 2017 08:33
Show Gist options
  • Save tsdtsdtsd/23e12a740935a4b22dedbc4f4238502c to your computer and use it in GitHub Desktop.
Save tsdtsdtsd/23e12a740935a4b22dedbc4f4238502c 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"
_ "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