給定一張圖片URL判斷圖片的大小和類型:FastImage
這是一個優秀 Ruby 庫FastImage通過獲取盡可能少的內容,對給定的一張圖片URI來判斷圖片的大小和類型。
特性
Fastimage還能夠讀取本地文件,使用Addressable庫來實現。
FastImage will automatically read from any object that responds to :read – for
instance an IO object if that is passed instead of a URI.
FastImage會跟進至4 HTTP重定向來獲得圖像。
FastImage will obey the http_proxy setting in your environment to route requests via a proxy.
You can add a timeout to the request which will limit the request time by passing :timeout => number_of_seconds.
FastImage normally replies will nil if it encounters an error, but you can pass :raise_on_failure => true to get an exception.
FastImage.size("http://stephensykes.com/images/ss.com_x.gif") => [266, 56] # width, height FastImage.type("http://stephensykes.com/images/pngimage") => :png FastImage.type("/some/local/file.gif") => :gif FastImage.size("http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg", :raise_on_failure=>true, :timeout=>0.1) => FastImage::ImageFetchFailure: FastImage::ImageFetchFailure FastImage.size("http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg", :raise_on_failure=>true, :timeout=>2.0) => [9545, 6623]github項目地址:https://github.com/sdsykes/fastimage
Python語言實現版:
https://github.com/bmuller/fastimage
用法
This assumes you have a working familiarity with Twisted.
Usage is pretty dang simple.
from fastimage import get_size
from twisted.internet import reactor
def handle(result):
width, height = result
print "Size: ", width, "x", height
reactor.stop()
url = "http://stephensykes.com/images/ss.com_x.gif"
get_size(url).addCallbacks(handle)
reactor.run()
which will poop out:Size: 266 x 56
You can also just get the image type:
from fastimage import get_type from twisted.internet import reactor def handle(result): print "Type:", result reactor.stop() url = "http://stephensykes.com/images/ss.com_x.gif" get_type(url).addCallbacks(handle) reactor.run()
which will poop out:
Type: gif
Right now, sizes can be deduced from gif / jpg / png. Additionally, it can detect the type (but not size) of bmp / tiff.