[Awesome Ruby Gem] Use fastimage gem to find the size or type of an image given its uri by fetching as little as needed

FastImage

FastImage finds the size or type of an image given its uri by fetching as little as needed

The problem

Your app needs to find the size or type of an image. This could be for adding width and height attributes to an image tag, for adjusting layouts or overlays to fit an image or any other of dozens of reasons.

But the image is not locally stored – it’s on another asset server, or in the cloud – at Amazon S3 for example.

You don’t want to download the entire image to your app server – it could be many tens of kilobytes, or even megabytes just to get this information. For most common image types (GIF, PNG, BMP etc.), the size of the image is simply stored at the start of the file. For JPEG files it’s a little bit more complex, but even so you do not need to fetch much of the image to find the size.

FastImage does this minimal fetch for image types GIF, JPEG, PNG, TIFF, BMP, ICO, CUR, PSD, SVG and WEBP. And it doesn’t rely on installing external libraries such as RMagick (which relies on ImageMagick or GraphicsMagick) or ImageScience (which relies on FreeImage).

You only need supply the uri, and FastImage will do the rest.

Features

FastImage can also read local (and other) files – anything that is not parseable as a URI will be interpreted as a filename, and FastImage will attempt to open it with File#open.

FastImage will also 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.

FastImage will obey the http_proxy setting in your environment to route requests via a proxy. You can also pass a :proxy argument if you want to specify the proxy address in the call.

You can add a timeout to the request which will limit the request time by passing :timeout => number_of_seconds.

FastImage normally replies with nil if it encounters an error, but you can pass :raise_on_failure => true to get an exception.

FastImage also provides a reader for the content length header provided in HTTP. This may be useful to assess the file size of an image, but do not rely on it exclusively – it will not be present in chunked responses for instance.

FastImage accepts additional HTTP headers. This can be used to set a user agent or referrer which some servers require. Pass an :http_header argument to specify headers, e.g., :http_header => {‘User-Agent’ => ‘Fake Browser’}.

FastImage can give you information about the parsed display orientation of an image with Exif data (jpeg or tiff).

FastImage also handles Data URIs correctly.

Installation

You can install it as a gem:

1
$ gem install fastimage

or add it into a Gemfile (Bundler):

1
2
3
4
5
# Gemfile

# sdsykes/fastimage: FastImage finds the size or type of an image given its uri by fetching as little as needed
# https://github.com/sdsykes/fastimage
gem 'fastimage', '2.2.5

Then, run bundle install.

1
$ bundle install

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require 'fastimage'

FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
=> [266, 56] # width, height
FastImage.type("http://stephensykes.com/images/pngimage")
=> :png
FastImage.type("/some/local/file.gif")
=> :gif
FastImage.size("http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg", :raise_on_failure=>true, :timeout=>0.1)
=> FastImage::ImageFetchFailure: FastImage::ImageFetchFailure
FastImage.size("http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_1350692_33_images.jpg", :raise_on_failure=>true, :timeout=>2.0)
=> [9545, 6623]
FastImage.new("http://stephensykes.com/images/pngimage").content_length
=> 432
FastImage.size("http://stephensykes.com/images/ss.com_x.gif", :http_header => {'User-Agent' => 'Fake Browser'})
=> [266, 56]
FastImage.new("http://stephensykes.com/images/ExifOrientation3.jpg").orientation
=> 3
FastImage.size("data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")
=> [1, 1]

References

[1] sdsykes/fastimage: FastImage finds the size or type of an image given its uri by fetching as little as needed - https://github.com/sdsykes/fastimage

[2] fastimage | RubyGems.org | your community gem host - https://rubygems.org/gems/fastimage/

[3] Find jpeg dimensions fast in pure Ruby, no image library needed | Pennysmalls - https://pennysmalls.wordpress.com/2008/08/19/find-jpeg-dimensions-fast-in-pure-ruby-no-ima/

[4] Getting JPG dimensions with AS3 without loading the entire file – Antti Kupila - http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/

[5] remvee/exifr: EXIF Reader - https://github.com/remvee/exifr