Created
January 29, 2015 07:56
-
-
Save madevelopers/694e9a1a6244df8128de to your computer and use it in GitHub Desktop.
golang: Get image dimension
This file contains hidden or 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 ( | |
| "fmt" | |
| "image" | |
| _ "image/gif" | |
| _ "image/jpeg" | |
| _ "image/png" | |
| "io" | |
| "os" | |
| ) | |
| func getImageDimension(r io.Reader) (int, int) { | |
| config, _, err := image.DecodeConfig(r) | |
| if err != nil { | |
| panic(err) | |
| } | |
| return config.Width, config.Height | |
| } | |
| func main() { | |
| f, err := os.Open("image-400x300.png") | |
| if err != nil { | |
| panic(err) | |
| } | |
| defer f.Close() | |
| w, h := getImageDimension(f) | |
| fmt.Printf("Width: %d\nHeight: %d\n", w, h) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment