Categorygithub.com/leeyifei/fastimage
repositorypackage
0.0.0-20210324095713-b807a0d2807c
Repository: https://github.com/leeyifei/fastimage.git
Documentation: pkg.go.dev

# README

fastimage

Golang implementation of fastimage.

Finds the type and/or size of an image given its uri by fetching as little as needed.

RECREATED and IMPROVED fastimage for golang.

Started from Ruben Fonseca (@rubenfonseca) 's fastimage https://github.com/rubenfonseca/fastimage

Included TIFF and WEBP support from https://github.com/golang/image

How?

fastimage parses the image data as it is downloaded. As soon as it finds out the size and type of the image, it stops the download.

Install

$ go get github.com/sillydong/fastimage

Usage

For instance, this is a big 10MB JPEG image on wikipedia:

Method1

customHeaders := map[string]string{
    "X-SECRET-HEADER": "your-header-value"
}
instance := fastimage.NewFastImage(2, customHeaders)
//leave it to nil to use default header settings
//eg. 
//instance := fastimage.NewFastImage(2, nil)

url1 := "http://upload.wikimedia.org/wikipedia/commons/9/9a/SKA_dishes_big.jpg"
imagetype1, size1, err1 := instance.Detect(url1)
fmt.Printf("%+v\t%+v\t%+v\n", imagetype1, size1, err1)

url2 := "http://upload.wikimedia.org/wikipedia/commons/9/9a/SKA_dishes_big.jpg"
imagetype2, size2, err2 := instance.Detect(url2)
fmt.Printf("%+v\t%+v\t%+v\n", imagetype2, size2, err2)

Method2

url1 := "http://upload.wikimedia.org/wikipedia/commons/9/9a/SKA_dishes_big.jpg"
imagetype1, size1, err1 := fastimage.GetImageSize(url1)
fmt.Printf("%+v\t%+v\t%+v\n", imagetype1, size1, err1)

url2 := "http://upload.wikimedia.org/wikipedia/commons/9/9a/SKA_dishes_big.jpg"
imagetype2, size2, err2 := fastimage.GetImageSize(url2)
fmt.Printf("%+v\t%+v\t%+v\n", imagetype2, size2, err2)

Notice

Method1 is better to detect multiple images because it shares one *http.Client.

Supported file types

File typeCan detect type?Can detect size?
PNGYesYes
JPEGYesYes
GIFYesYes
BMPYesYes
TIFFYesYes
WEBPYesYes

Notice

Some webp images' Content-Type in header is text/plain. Here in this library we only parse response data when Content-Type has keyword "image" in.

TODO

I'm thinking about using valyala/fasthttp to replace net/http to get better network performance.

License

fastimage is under MIT license. See the LICENSE file for details.