Rails Plugins: attachment_fu

I’ve used Rick Olson‘s excellent acts_as_attachment on a number of projects and it’s saved me a huge amount of time that would have been spent worrying about how best to resize images, how to make sure uploaded files are properly written to storage, and other such boring details. So I’ve been noticing with interest increasing references to his new attachment_fu plugin, which is a complete rewrite of acts_as_attachment.

That interest was piqued as I skimmed this nice how-to on file attachment from Mike Clark (if you’re just looking for how to get started, go read that). It seems attachment_fu is almost entirely backwards compatible with acts_as_attachment (you just need to change your models to rename the ‘acts_as_attachment’ declaration ‘has_attachment’), but with a number of enhancements.

First up is the addition of pluggable ‘processors.’ Where acts_as_attachment relied on RMagick to manipulate images (useful things like creating thumbnails), attachment_fu supports ImageScience and MiniMagick, both of which are much lighter on resources than RMagick. And because each processor is implemented as its own module, you can easily add in any image processing library you can write a wrapper for.

So say you wanted to use a custom processor contained in the module Technoweenie::AttachmentFu::Processors::MyCustomProcessor, your model might look like:

class Avatar  :image,
                 :processor => :my_custom,
                 :max_size => 500.kilobytes,
                 :resize_to => '240x240>',
                 :thumbnails => { :thumb => '100x100>' }

  validates_as_attachment
end

Similarly, the storage code is also now pluggable. As Mike Clark explains in his tutorial, the first fruits of that is an S3 storage option, allowing all your uploaded files to be stored in your Amazon S3 account. But there’s no reason to stop there. To add your own storage option you’d just need a compatible module and to declare it just as you would a custom processor. For Technoweenie::AttachmentFu::Backends::MyCustomBackend the model would look like:

class Avatar  :image,
                 :storage => :my_custom,
                 :max_size => 500.kilobytes,
                 :resize_to => '240x240>',
                 :thumbnails => { :thumb => '100x100>' }

  validates_as_attachment
end

attachment_fu is only compatible with Rails 1.2+ so for those legacy projects still on older versions of Rails, it’s not an option. But for new projects, the ability to use other processors looks like a definite win, and the pluggable architecture is a nice bonus.

Tags: , , , , , ,

3 comments

  1. Have you written a custom processor (something not related to image processing). Would be great to have a look at the code if you did.

  2. I’m afraid I haven’t. The default processors have served me fine so far. It should be fairly straightforward to follow the code in the defaults and modify from there.

  3. @Warren – I just discovered one writeup that you may find helpful. It describes writing an Apple Core Image adapter for attachment_fu

    http://redartisan.com/2007/12/12/attachment-fu-with-core-image