Class: FastImage
- Inherits:
-
Object
- Object
- FastImage
- Defined in:
- lib/fastimage.rb
Defined Under Namespace
Modules: StreamUtil Classes: CannotParseImage, Exif, FastImageException, FiberStream, IOStream, ImageFetchFailure, SizeNotFound, UnknownImageType
Constant Summary collapse
- DefaultTimeout =
2
- LocalFileChunkSize =
256
Instance Attribute Summary collapse
-
#bytes_read ⇒ Object
readonly
Returns the value of attribute bytes_read.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .parse_uri(location) ⇒ Object
-
.size(uri, options = {}) ⇒ Object
Returns an array containing the width and height of the image.
-
.type(uri, options = {}) ⇒ Object
Returns an symbol indicating the image type fetched from a uri.
-
.uri_parser=(parser) ⇒ Object
Parser object should respond to #parse and raise a URI::InvalidURIError if something goes wrong.
-
.use_addressable_uri_parser ⇒ Object
Helper that sets URI parsing to use the Addressable gem.
Instance Method Summary collapse
-
#initialize(uri, options = {}) ⇒ FastImage
constructor
A new instance of FastImage.
Constructor Details
#initialize(uri, options = {}) ⇒ FastImage
Returns a new instance of FastImage.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/fastimage.rb', line 182 def initialize(uri, ={}) @property = [:type_only] ? :type : :size @timeout = [:timeout] || DefaultTimeout @uri = uri if uri.respond_to?(:read) fetch_using_read(uri) else begin @parsed_uri = self.class.parse_uri(uri) rescue URI::InvalidURIError fetch_using_open_uri else if @parsed_uri.scheme == "http" || @parsed_uri.scheme == "https" fetch_using_http else fetch_using_open_uri end end end uri.rewind if uri.respond_to?(:rewind) raise SizeNotFound if [:raise_on_failure] && @property == :size && !@size rescue Timeout::Error, SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET, ImageFetchFailure, Net::HTTPBadResponse, EOFError, Errno::ENOENT raise ImageFetchFailure if [:raise_on_failure] rescue NoMethodError # 1.8.7p248 can raise this due to a net/http bug raise ImageFetchFailure if [:raise_on_failure] rescue UnknownImageType raise UnknownImageType if [:raise_on_failure] rescue CannotParseImage if [:raise_on_failure] if @property == :size raise SizeNotFound else raise ImageFetchFailure end end end |
Instance Attribute Details
#bytes_read ⇒ Object (readonly)
Returns the value of attribute bytes_read.
53 54 55 |
# File 'lib/fastimage.rb', line 53 def bytes_read @bytes_read end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
51 52 53 |
# File 'lib/fastimage.rb', line 51 def size @size end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
51 52 53 |
# File 'lib/fastimage.rb', line 51 def type @type end |
Class Method Details
.parse_uri(location) ⇒ Object
88 89 90 |
# File 'lib/fastimage.rb', line 88 def self.parse_uri(location) (@uri_parser || URI).parse(location) end |
.size(uri, options = {}) ⇒ Object
Returns an array containing the width and height of the image. It will return nil if the image could not be fetched, or if the image type was not recognised.
By default there is a timeout of 2 seconds for opening and reading from a remote server. This can be changed by passing a :timeout => number_of_seconds in the options.
If you wish FastImage to raise if it cannot size the image for any reason, then pass :raise_on_failure => true in the options.
FastImage knows about GIF, JPEG, BMP, TIFF, ICO, CUR, PNG, PSD and WEBP files.
Example
require 'fastimage'
FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
=> [266, 56]
FastImage.size("http://stephensykes.com/images/pngimage")
=> [16, 16]
FastImage.size("http://farm4.static.flickr.com/3023/3047236863_9dce98b836.jpg")
=> [500, 375]
FastImage.size("http://www-ece.rice.edu/~wakin/images/lena512.bmp")
=> [512, 512]
FastImage.size("test/fixtures/test.jpg")
=> [882, 470]
FastImage.size("http://pennysmalls.com/does_not_exist")
=> nil
FastImage.size("http://pennysmalls.com/does_not_exist", :raise_on_failure=>true)
=> raises FastImage::ImageFetchFailure
FastImage.size("http://stephensykes.com/favicon.ico", :raise_on_failure=>true)
=> [16, 16]
FastImage.size("http://stephensykes.com/images/squareBlue.icns", :raise_on_failure=>true)
=> raises FastImage::UnknownImageType
FastImage.size("http://stephensykes.com/favicon.ico", :raise_on_failure=>true, :timeout=>0.01)
=> raises FastImage::ImageFetchFailure
FastImage.size("http://stephensykes.com/images/faulty.jpg", :raise_on_failure=>true)
=> raises FastImage::SizeNotFound
Supported options
- :timeout
-
Overrides the default timeout of 2 seconds. Applies both to reading from and opening the http connection.
- :raise_on_failure
-
If set to true causes an exception to be raised if the image size cannot be found for any reason.
136 137 138 |
# File 'lib/fastimage.rb', line 136 def self.size(uri, ={}) new(uri, ).size end |
.type(uri, options = {}) ⇒ Object
Returns an symbol indicating the image type fetched from a uri. It will return nil if the image could not be fetched, or if the image type was not recognised.
By default there is a timeout of 2 seconds for opening and reading from a remote server. This can be changed by passing a :timeout => number_of_seconds in the options.
If you wish FastImage to raise if it cannot find the type of the image for any reason, then pass :raise_on_failure => true in the options.
Example
require 'fastimage'
FastImage.type("http://stephensykes.com/images/ss.com_x.gif")
=> :gif
FastImage.type("http://stephensykes.com/images/pngimage")
=> :png
FastImage.type("http://farm4.static.flickr.com/3023/3047236863_9dce98b836.jpg")
=> :jpeg
FastImage.type("http://www-ece.rice.edu/~wakin/images/lena512.bmp")
=> :bmp
FastImage.type("test/fixtures/test.jpg")
=> :jpeg
FastImage.type("http://stephensykes.com/does_not_exist")
=> nil
File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
=> :gif
FastImage.type("test/fixtures/test.tiff")
=> :tiff
FastImage.type("test/fixtures/test.psd")
=> :psd
Supported options
- :timeout
-
Overrides the default timeout of 2 seconds. Applies both to reading from and opening the http connection.
- :raise_on_failure
-
If set to true causes an exception to be raised if the image type cannot be found for any reason.
178 179 180 |
# File 'lib/fastimage.rb', line 178 def self.type(uri, ={}) new(uri, .merge(:type_only=>true)).type end |
.uri_parser=(parser) ⇒ Object
Parser object should respond to #parse and raise a URI::InvalidURIError if something goes wrong
72 73 74 |
# File 'lib/fastimage.rb', line 72 def self.uri_parser=(parser) @uri_parser = parser end |
.use_addressable_uri_parser ⇒ Object
Helper that sets URI parsing to use the Addressable gem
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fastimage.rb', line 77 def self.use_addressable_uri_parser require 'addressable/uri' self.uri_parser = Class.new do def self.parse(location) Addressable::URI.parse(location) rescue Addressable::URI::InvalidURIError raise URI::InvalidURIError end end end |