FastImage finds image dimensions fast using minimal resources

I just released a gem to find image dimensions and type information fast. I have previously done some work in this area, but this is a much more comprehensive solution, and fixes problems with certain jpegs.

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 image types, 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 most of the image to find the size.

FastImage does this minimal fetch for image types GIF, JPEG, PNG and BMP. 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.

Examples

require ‘fastimage’

FastImage.size("http://stephensykes.com/images/ss.com_x.gif")
=> [266, 56]  # width, height
FastImage.type("http://stephensykes.com/images/pngimage")
=> :png

Installation

Gem

sudo gem install sdsykes-fastimage -s http://gems.github.com

Rails

Install the gem as above, and configure it in your environment.rb file as below:


Rails::Initializer.run do |config|

config.gem "sdsykes-fastimage", :lib=>"fastimage"

end

Then you’re off – just use FastImage.size() and FastImage.type() in your code as in the examples.

Documentation

http://rdoc.info/projects/sdsykes/fastimage

10 comments for this post.

  1. Comment from jason | 12 June 2009 | 6:38 am :

    Nice work!

    Would it possible to use a more general URL? So local files are treated in the same way?

  2. Comment from Michael Sheakoski | 12 June 2009 | 8:48 am :

    Another project you may be interested in is http://github.com/andersonbrandon/ruby-imagespec

    It gives you the dimensions and content-type of PNG, GIF, JPG, and SWF (Flash movies). You can pass to it a URL, file handle, or a string containing the path to a file.

  3. Comment from Stephen Sykes | 12 June 2009 | 9:33 am :

    Jason: yes, I’ll add that feature

    Michael: that uses open-uri to fetch the url – I wonder if it fetches only the minimum necessary to get the size. I’ll test it.

  4. Comment from Vesa Nieminen | 16 June 2009 | 1:00 pm :

    Great project. Thanks!

  5. Comment from Stephen Sykes | 25 June 2009 | 11:27 am :

    Jason: Version 1.1.1 was just released, and supports reading from local files.

    Vesa: Ole hyvä!

  6. Comment from Michael Sheakoski | 25 June 2009 | 5:38 pm :

    Stephen: As of right now I have open-uri just download the whole file. It was a quick hack since I mostly use the lib for local files. I’ll check out fastimage.

  7. Comment from jason | 29 June 2009 | 1:00 pm :

    Excellent thanks! :)

  8. Comment from Adriel Dang | 8 May 2010 | 3:50 am :

    Hi, I’ve tried this code:

    FastImage.size(“http://www.thanhnien.com.vn/News/Picture201001/lop10.jpg”)
    => nil

    I can load the image by FireFox, and see its size as 250×188. What’s the problem?

  9. Comment from Stephen Sykes | 8 May 2010 | 4:05 pm :

    It’s a bug – thanks for the report. Please install version 1.2.6 using sudo gem install fastimage

  10. Comment from Adriel Dang | 8 May 2010 | 4:35 pm :

    It works! Thank you. ^^

Leave your comment...

Powered by WP Hashcash

Blog Archives

Navigation


About this blog

A blog about Ruby, Rails and other tech. Mostly.


Find Something?