给定一张图片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.