fastimage

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

  • Owner: sdsykes/fastimage
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

!https://img.shields.io/gem/dt/fastimage.svg!:https://rubygems.org/gems/fastimage
!https://travis-ci.org/sdsykes/fastimage.svg?branch=master!:https://travis-ci.org/sdsykes/fastimage

h1. FastImage

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

h2. 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.

h2. 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":https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs correctly.

h2. Security

As of v1.6.7 FastImage no longer uses @openuri@ to open files, but directly calls @File.open@. Take care to sanitise the strings passed to FastImage; it will try to read from whatever is passed.

h2. Examples

h2. Installation

h4. Required Ruby version

FastImage version 2.0.0 and above work with Ruby 1.9.2 and above.

FastImage version 1.9.0 was the last version that supported Ruby 1.8.7.

h4. Gem

bc. gem install fastimage

h4. Rails

Add fastimage to your Gemfile, and bundle.

bc. gem 'fastimage'

Then you're off - just use @FastImage.size()@ and @FastImage.type()@ in your code as in the examples.

h2. Documentation

"http://sdsykes.github.io/fastimage/rdoc/FastImage.html":http://sdsykes.github.io/fastimage/rdoc/FastImage.html

h2. Maintainer

FastImage is maintained by Stephen Sykes (@sdsykes). Support this project by using "LibPixel":https://libpixel.com cloud based image resizing and processing service.

h2. Benchmark

It's way faster than conventional methods (for example the image_size gem) for most types of file when fetching over the wire.

The file is fetched in about 7.5 seconds in this test (the number in brackets is the total time taken), but as FastImage doesn't need to fetch the whole thing, it completes in less than 0.1s.

You'll see similar excellent results for the other file types, except for TIFF. Unfortunately TIFFs tend to have their
metadata towards the end of the file, so it makes little difference to do a minimal fetch. The result shown below is
mostly dependent on the exact internet conditions during the test, and little to do with the library used.

h2. Tests

You'll need to @gem install fakeweb@ and possibly also @gem install test-unit@ to be able to run the tests.

bc.. $ ruby test.rb
Run options:

Running tests:

Finished tests in 1.033640s, 23.2189 tests/s, 82.2337 assertions/s.
24 tests, 85 assertions, 0 failures, 0 errors, 0 skips

h2. References

  • "Pennysmalls - Find jpeg dimensions fast in pure Ruby, no image library needed":http://pennysmalls.wordpress.com/2008/08/19/find-jpeg-dimensions-fast-in-pure-ruby-no-ima/
  • "Antti Kupila - Getting JPG dimensions with AS3 without loading the entire file":http://www.anttikupila.com/flash/getting-jpg-dimensions-with-as3-without-loading-the-entire-file/
  • "imagesize gem":https://rubygems.org/gems/imagesize
  • "EXIF Reader":https://github.com/remvee/exifr

h2. FastImage in other languages

  • "Python by bmuller":https://github.com/bmuller/fastimage
  • "Swift by kaishin":https://github.com/kaishin/ImageScout
  • "Go by rubenfonseca":https://github.com/rubenfonseca/fastimage
  • "PHP by tommoor":https://github.com/tommoor/fastimage
  • "Node.js by ShogunPanda":https://github.com/ShogunPanda/fastimage
  • "Objective C by kylehickinson":https://github.com/kylehickinson/FastImage
  • "Android by qstumn":https://github.com/qstumn/FastImageSize

h2. Licence

MIT, see file "MIT-LICENSE":MIT-LICENSE

Main metrics

Overview
Name With Ownersdsykes/fastimage
Primary LanguageRuby
Program languageRuby (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2009-06-10 13:53:18
Pushed At2025-01-03 16:41:06
Last Commit At2025-01-03 16:28:50
Release Count41
Last Release Name2.4.0 (Posted on )
First Release Namev1.0.1 (Posted on )
用户参与
Stargazers Count1.4k
Watchers Count25
Fork Count116
Commits Count376
Has Issues Enabled
Issues Count84
Issue Open Count0
Pull Requests Count61
Pull Requests Open Count0
Pull Requests Close Count11
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private