New to Rails 3? Check out the Ruby on Rails 3 Tutorial book and screencast.

A book and screencast series showing you how to develop and deploy industrial-strength Rails apps in a direct, step by step way. The screencast series includes 12 lessons over more than 15 hours! Get the best "over the shoulder" experience of following what a top Rails 3 developer does when building an app today. Click here to learn more.

Dragonfly: Image Handling For Champions

In Plugins

dragonfly.jpgDragonfly is a new(-ish!) ruby gem for handling images and other content in Ruby web apps (including Rails). “What – another one?!”.. I hear you exclaim. While using Dragonfly in Rails is similar to using, say, Paperclip, a significant difference is that images are generated on the fly, as opposed to when uploaded. This at first sounds like just a small detail, but it opens us up to a wide range of new possibilities..

First, thumbnail sizes are no longer specified in the model, but in the view. So when designs (i.e. image sizes) change, we simply make the change…in the view. Previously we’d change the model, then reprocess all of the already uploaded images. Furthermore, it is now trivial to have a custom avatar cropping for each user, or blog posts with different image sizes, etc. This would be difficult if thumbnails were created on upload.

Rack Off

Dragonfly isn't so much for Rails, but for anything using Rack. It can be used as a standalone Rack application, or inserted as middleware into any Rack stack, e.g. with Sinatra, Merb, etc. Using with Rails is designed to be super-easy though, so anyone used to using Paperclip and friends should find it fairly intuitive.

Custom and Excise

Custom data stores / processors / encoders / analysers, etc., can be created to suit your needs. It is designed to be as configurable as possible, while using sensible defaults for those who can’t be bothered with the details. Any type data can be served – not just images – though not all data suits the on-the-fly paradigm.

Cache and Carry

Dragonfly is meant to be used behind a cache-proxy such as Varnish, Squid or Rack::Cache, so that while the first request may take some time, subsequent requests should be super-quick!

Take a look at the bamboo blog post for an introduction, or else check out the code and the documentation

[post by] Mark Evans, a web developer at New Bamboo who writes Ruby and Javascript by day and spends the rest of the time running around like a headless chicken and playing the guitar.

Post to Twitter Tweet This Post

Vaguely Related Posts (Usually)

5 Comment Responses to “Dragonfly: Image Handling For Champions”

  1. #1
    Eric Anderson Says:

    The old "file_column" plugin used to do this. Actually it allowed either/or. You could specify in the model and have it generated on upload OR you could have it specified in the view and have it generated on demand.

    Will check this out because although I like the flexible storage and processing options of Paperclip I miss the on-demand nature of file_column.

  2. #2
    Mark Evans Says:

    I didn't realise that - I thought file_column was on-upload only - will take a look!
    There's another one called Fleximage which has been around for a while and looks quite decent.

    I guess part of the reason I felt the need to write Dragonfly though was that those are for Rails only, and I wanted something that worked outside of Rails (i.e. a Rack-based gem). I also wanted more ability to customize the different parts.

  3. #3
    Jaime Iniesta Says:

    That's definitely a good idea, specially when you need to regenerate all thumbnails after introducing new styles -- that can take hours on large sites.

    I had my doubts on the performance of this but as it is easily cached, this should be working fine. I will try it!

  4. #4
    Daniel Fischer Says:

    And... how would you use this on Heroku?

  5. #5
    Mark Evans Says:

    @Daniel - see http://markevans.github.com/dragonfly/file.ExampleUseCases.html

Leave a Reply