Class: FastImage

Inherits:
Object
  • Object
show all
Defined in:
lib/fastimage.rb

Defined Under Namespace

Classes: FastImageException, ImageFetchFailure, MoreCharsNeeded, SizeNotFound, UnknownImageType

Constant Summary collapse

DefaultTimeout =
2
LocalFileChunkSize =
256

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}) ⇒ FastImage

Returns a new instance of FastImage.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/fastimage.rb', line 147

def initialize(uri, options={})
  @property = options[:type_only] ? :type : :size
  @timeout = options[:timeout] || DefaultTimeout
  @uri = uri

  if uri.respond_to?(:read)
    fetch_using_read(uri)
  else
    begin
      @parsed_uri = URI.parse(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
  raise SizeNotFound if options[: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 options[:raise_on_failure]
rescue NoMethodError  # 1.8.7p248 can raise this due to a net/http bug
  raise ImageFetchFailure if options[:raise_on_failure]
rescue UnknownImageType
  raise UnknownImageType if options[:raise_on_failure]
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



46
47
48
# File 'lib/fastimage.rb', line 46

def size
  @size
end

#typeObject (readonly)

Returns the value of attribute type.



46
47
48
# File 'lib/fastimage.rb', line 46

def type
  @type
end

Class Method Details

.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 and PNG 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)
=> 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.



105
106
107
# File 'lib/fastimage.rb', line 105

def self.size(uri, options={})
  new(uri, options).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://pennysmalls.com/does_not_exist")
=> nil
File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
=> :gif

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.



143
144
145
# File 'lib/fastimage.rb', line 143

def self.type(uri, options={})
  new(uri, options.merge(:type_only=>true)).type
end