Skip to content

Instantly share code, notes, and snippets.

@varver
Created September 4, 2015 13:02
Show Gist options
  • Save varver/cd8e56b7558164b1cff6 to your computer and use it in GitHub Desktop.
Save varver/cd8e56b7558164b1cff6 to your computer and use it in GitHub Desktop.
Get size and format of any image present online or on disk in golang (go)
package main
import (
"bytes"
"fmt"
"github.com/varver/rextro"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/webp"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
)
func main() {
ImageUrl := "http://www.axialis.com/tutorials/sample/logo.bmp"
req := rextro.NewTequest(ImageUrl)
imageBytesData, err := req.Fetch("GET")
if err != nil {
fmt.Println(err.Error())
}
/////////////////////////////////////////////////////////////////////////////
// you can also use code after this if you have a image file on disk instead
// just read image from disk and pass in bytes below.
////////////////////////////////////////////////////////////////////////////
// process image present as bytes
img, format, err := image.DecodeConfig(bytes.NewReader(imageBytesData))
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("Image Format :", format)
fmt.Println("Image Width :", img.Width)
fmt.Println("Image Hight :", img.Height)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment