Last Modified
2014-09-11 00:29:26 +0000
Requires
  • net/https
  • addressable/uri
  • fastimage/fbr.rb
  • delegate
  • pathname

Description

FastImage finds the size or type of an image given its uri. It is careful to only fetch and parse as much of the image as is needed to determine the result. It does this by using a feature of Net::HTTP that yields strings from the resource being fetched as soon as the packets arrive.

No external libraries such as ImageMagick are used here, this is a very lightweight solution to finding image information.

FastImage knows about GIF, JPEG, BMP, TIFF, PNG and PSD files.

FastImage can also read files from the local filesystem by supplying the path instead of a uri. In this case FastImage uses the Addressable library to read the file in chunks of 256 bytes until it has enough. This is possibly a useful bandwidth-saving feature if the file is on a network attached disk rather than truly local.

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 will follow up to 4 HTTP redirects to get the image.

Examples

require 'fastimage'

FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
=> [266, 56]
FastImage.type("http://stephensykes.com/images/pngimage")
=> :png
FastImage.type("/some/local/file.gif")
=> :gif
File.open("/some/local/file.gif", "r") {|io| FastImage.type(io)}
=> :gif

References