fastimage

golang版本的fastimage,脱胎于python的 fastimage.

能够在取尽可能少数据的情况下获取图片尺寸。

参考了Ruben Fonseca(@rubenfonseca)的fastimage库 https://github.com/rubenfonseca/fastimage

从golang官方的imagehttps://github.com/golang/image中取到了WEBP及TIFF图片尺寸解析的逻辑

How?

从网络按需读取数据,得到尺寸后即断开请求。

Install

$ go get github.com/sillydong/fastimage

Usage

取一张10MB的JPEG图片:

url := "http://upload.wikimedia.org/wikipedia/commons/9/9a/SKA_dishes_big.jpg"

imagetype, size, err := fastimage.DetectImageType(url)
if err != nil {
    // Something went wrong, http failed? not an image?
    panic(err)
}

switch imagetype {
case fastimage.JPEG:
    log.Printf("JPEG")
case fastimage.PNG:
    log.Printf("PNG")
case fastimage.GIF:
    log.Printf("GIF")
case fastimage.BMP:
    log.Printf("BMP")
case fastimage.WEBP:
    log.Printf("WEBP")
case fastimage.TIFF:
    log.Printf("TIFF")
}

log.Printf("Image type: %s", imagetype.String())
log.Printf("Image size: %v", size)

Supported file types

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

附PHP版本的fastimage

https://github.com/sillydong/phpfastimage