Skip to content

Instantly share code, notes, and snippets.

@rodkranz
Created July 19, 2018 18:14
Show Gist options
  • Save rodkranz/ea3c9367c0b0698c194897f127226388 to your computer and use it in GitHub Desktop.
Save rodkranz/ea3c9367c0b0698c194897f127226388 to your computer and use it in GitHub Desktop.
get information of image without download all image
package main
import (
_ "image/png"
_ "image/gif"
_ "image/jpeg"
"time"
"net/http"
"log"
"io"
"fmt"
"image"
"bytes"
"path"
"io/ioutil"
)
const BYTES_64K = 64 * 1 << 10
func main() {
//imageSize("http://localhost:8080/hh-us-lc-gopher-facts-habitat.png")
//imageSize("http://localhost:8080/8k-image.jpg")
//imageSize("http://localhost:8080/logo-vikings.png")
//imageSize("http://localhost:8080/image8k.jpg")
//imageSize("http://localhost:8080/image8k.jpg")
//imageSize("http://localhost:8080/gopher-open-graph-f175d940694bad1bc4e5fe3da49b6ac6.jpg")
imageSize("http://www.andrealeixo.com/images/home/andrealeixo.png")
}
func imageSize(imageURL string) {
start := time.Now()
res, err := http.Get(imageURL)
end := time.Since(start)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\n\n %#v \n\n", res)
buf := make([]byte, BYTES_64K)
io.ReadAtLeast(res.Body, buf, BYTES_64K)
res.Body.Close()
config, format, err := image.DecodeConfig(bytes.NewReader(buf))
if err != nil {
log.Fatal(err)
}
fmt.Println("./" + path.Base(imageURL))
err = ioutil.WriteFile("./"+path.Base(imageURL), buf, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Format: %s, Dimensions: %d x %d with time [%v] \n", format, config.Width, config.Height, end)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment