Class: FastImage

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

Defined Under Namespace

Modules: StreamUtil Classes: CannotParseImage, Exif, FastImageException, FiberStream, IOStream, ImageFetchFailure, SizeNotFound, Svg, UnknownImageType

Constant Summary collapse

LocalFileChunkSize =
256
VERSION =
"3.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FastImage.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/fastimage.rb', line 131

def initialize(source, options={})
  @source = source
  @options = {
    :type_only        => false,
    :raise_on_failure => false,
    :proxy            => nil,
    :http_header      => {}
  }.merge(options)

  @property = @options[:type_only] ? :type : :size

  if @source.respond_to?(:read)
    @path = @source.path if @source.respond_to? :path
    fetch_using_read
  else
    @path = @source
    fetch_using_file_open
  end

  raise SizeNotFound if @options[:raise_on_failure] && @property == :size && !@size

rescue ImageFetchFailure, EOFError, Errno::ENOENT
  raise ImageFetchFailure if @options[:raise_on_failure]
rescue UnknownImageType
  raise UnknownImageType if @options[:raise_on_failure]
rescue CannotParseImage
  if @options[:raise_on_failure]
    if @property == :size
      raise SizeNotFound
    else
      raise ImageFetchFailure
    end
  end

ensure
  source.rewind if source.respond_to?(:rewind)
end

Instance Attribute Details

#bytes_readObject (readonly)

Returns the value of attribute bytes_read.



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

def bytes_read
  @bytes_read
end

#orientationObject (readonly)

Returns the value of attribute orientation.



43
44
45
# File 'lib/fastimage.rb', line 43

def orientation
  @orientation
end

#pathObject (readonly)

Returns the value of attribute path.



43
44
45
# File 'lib/fastimage.rb', line 43

def path
  @path
end

#sizeObject (readonly)

Returns the value of attribute size.



43
44
45
# File 'lib/fastimage.rb', line 43

def size
  @size
end

#sourceObject (readonly)

Returns the value of attribute source.



43
44
45
# File 'lib/fastimage.rb', line 43

def source
  @source
end

#typeObject (readonly)

Returns the value of attribute type.



43
44
45
# File 'lib/fastimage.rb', line 43

def type
  @type
end

Class Method Details

.size(source, 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.

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, SVG and WEBP files.

Example

require 'fastimage'

FastImage.size("example.gif")
=> [266, 56]
FastImage.size("does_not_exist")
=> nil
FastImage.size("does_not_exist", :raise_on_failure => true)
=> raises FastImage::ImageFetchFailure
FastImage.size("example.png", :raise_on_failure => true)
=> [16, 16]
FastImage.size("app.icns", :raise_on_failure=>true)
=> raises FastImage::UnknownImageType
FastImage.size("faulty.jpg", :raise_on_failure=>true)
=> raises FastImage::SizeNotFound

Supported options

:raise_on_failure

If set to true causes an exception to be raised if the image size cannot be found for any reason.



91
92
93
# File 'lib/fastimage.rb', line 91

def self.size(source, options={})
  new(source, options).size
end

.type(source, options = {}) ⇒ Object

Returns an symbol indicating the image type located at source. It will return nil if the image could not be fetched, or if the image type was not recognised.

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("example.gif")
=> :gif
FastImage.type("image.png")
=> :png
FastImage.type("photo.jpg")
=> :jpeg
FastImage.type("lena512.bmp")
=> :bmp
FastImage.type("does_not_exist")
=> nil
File.open("file.gif", "r") {|io| FastImage.type(io)}
=> :gif
FastImage.type("test/fixtures/test.tiff")
=> :tiff
FastImage.type("test/fixtures/test.psd")
=> :psd

Supported options

:raise_on_failure

If set to true causes an exception to be raised if the image type cannot be found for any reason.



127
128
129
# File 'lib/fastimage.rb', line 127

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