Class: FastImage
- Inherits:
-
Object
- Object
- FastImage
- Defined in:
- lib/fastimage.rb,
lib/fastimage/version.rb
Defined Under Namespace
Modules: StreamUtil Classes: BadImageURI, CannotParseImage, Exif, FastImageException, FiberStream, Gif, IOStream, ImageFetchFailure, IsoBmff, SizeNotFound, Svg, UnknownImageType
Constant Summary collapse
- DefaultTimeout =
2
- LocalFileChunkSize =
256
- SUPPORTED_IMAGE_TYPES =
[:bmp, :gif, :jpeg, :png, :tiff, :psd, :heic, :heif, :webp, :svg, :ico, :cur].freeze
- VERSION =
'2.3.1'
Instance Attribute Summary collapse
-
#animated ⇒ Object
readonly
Returns the value of attribute animated.
-
#bytes_read ⇒ Object
readonly
Returns the value of attribute bytes_read.
-
#content_length ⇒ Object
readonly
Returns the value of attribute content_length.
-
#orientation ⇒ Object
readonly
Returns the value of attribute orientation.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.animated?(uri, options = {}) ⇒ Boolean
Returns a boolean value indicating the image is animated.
-
.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.
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.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/fastimage.rb', line 216 def initialize(uri, ={}) @uri = uri @options = { :type_only => false, :timeout => DefaultTimeout, :raise_on_failure => false, :proxy => nil, :http_header => {} }.merge() @property = if @options[:animated_only] :animated elsif @options[:type_only] :type else :size end raise BadImageURI if uri.nil? @type, @state = nil if uri.respond_to?(:read) fetch_using_read(uri) elsif uri.start_with?('data:') fetch_using_base64(uri) else begin @parsed_uri = URI.parse(uri) rescue URI::InvalidURIError fetch_using_file_open else if @parsed_uri.scheme == "http" || @parsed_uri.scheme == "https" fetch_using_http else fetch_using_file_open end end end raise SizeNotFound if @options[:raise_on_failure] && @property == :size && !@size rescue Timeout::Error, SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET, Errno::ENETUNREACH, ImageFetchFailure, Net::HTTPBadResponse, EOFError, Errno::ENOENT, OpenSSL::SSL::SSLError raise ImageFetchFailure if @options[:raise_on_failure] rescue UnknownImageType, BadImageURI raise if @options[:raise_on_failure] rescue CannotParseImage if @options[:raise_on_failure] if @property == :size raise SizeNotFound else raise ImageFetchFailure end end ensure uri.rewind if uri.respond_to?(:rewind) end |
Instance Attribute Details
#animated ⇒ Object (readonly)
Returns the value of attribute animated.
75 76 77 |
# File 'lib/fastimage.rb', line 75 def animated @animated end |
#bytes_read ⇒ Object (readonly)
Returns the value of attribute bytes_read.
77 78 79 |
# File 'lib/fastimage.rb', line 77 def bytes_read @bytes_read end |
#content_length ⇒ Object (readonly)
Returns the value of attribute content_length.
75 76 77 |
# File 'lib/fastimage.rb', line 75 def content_length @content_length end |
#orientation ⇒ Object (readonly)
Returns the value of attribute orientation.
75 76 77 |
# File 'lib/fastimage.rb', line 75 def orientation @orientation end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
75 76 77 |
# File 'lib/fastimage.rb', line 75 def size @size end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
75 76 77 |
# File 'lib/fastimage.rb', line 75 def type @type end |
Class Method Details
.animated?(uri, options = {}) ⇒ Boolean
Returns a boolean value indicating the image is animated. 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.animated?("test/fixtures/test.gif")
=> false
FastImage.animated?("test/fixtures/animated.gif")
=> true
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.
212 213 214 |
# File 'lib/fastimage.rb', line 212 def self.animated?(uri, ={}) new(uri, .merge(:animated_only=>true)).animated 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, SVG and WEBP files.
Example
require 'fastimage'
FastImage.size("https://switchstep.com/images/ios.gif")
=> [196, 283]
FastImage.size("http://switchstep.com/images/ss_logo.png")
=> [300, 300]
FastImage.size("https://upload.wikimedia.org/wikipedia/commons/0/09/Jpeg_thumb_artifacts_test.jpg")
=> [1280, 800]
FastImage.size("https://eeweb.engineering.nyu.edu/~yao/EL5123/image/lena_gray.bmp")
=> [512, 512]
FastImage.size("test/fixtures/test.jpg")
=> [882, 470]
FastImage.size("http://switchstep.com/does_not_exist")
=> nil
FastImage.size("http://switchstep.com/does_not_exist", :raise_on_failure=>true)
=> raises FastImage::ImageFetchFailure
FastImage.size("http://switchstep.com/images/favicon.ico", :raise_on_failure=>true)
=> [16, 16]
FastImage.size("http://switchstep.com/foo.ics", :raise_on_failure=>true)
=> raises FastImage::UnknownImageType
FastImage.size("http://switchstep.com/images/favicon.ico", :raise_on_failure=>true, :timeout=>0.01)
=> raises FastImage::ImageFetchFailure
FastImage.size("http://switchstep.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.
142 143 144 |
# File 'lib/fastimage.rb', line 142 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("https://switchstep.com/images/ios.gif")
=> :gif
FastImage.type("http://switchstep.com/images/ss_logo.png")
=> :png
FastImage.type("https://upload.wikimedia.org/wikipedia/commons/0/09/Jpeg_thumb_artifacts_test.jpg")
=> :jpeg
FastImage.type("https://eeweb.engineering.nyu.edu/~yao/EL5123/image/lena_gray.bmp")
=> :bmp
FastImage.type("test/fixtures/test.jpg")
=> :jpeg
FastImage.type("http://switchstep.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.
184 185 186 |
# File 'lib/fastimage.rb', line 184 def self.type(uri, ={}) new(uri, .merge(:type_only=>true)).type end |