Setting up attachment_fu for Rails with ImageScience on a Mac

Posted on 05 May 2008 by Johannes Fahrenkrug. Tags: programming tutorials rails
Who doesn't need file uploads in their Rails app? Lots of people, but I'm unfortunately not one of them. No need to reinvent the wheel, though since there's a spiffy plugin called attachment_fu by Rails core contributor Rick Olsen aka techno weenie. How to set this sucker up on Mac OS? Easy: Update:Digging through the source of the attachment_fu plugin I found something very cool that's not mentioned in the README: it comes with a CoreImage image processor. So if you use a current version of Mac OS X you can skip straight to step 7. Attachment_fu will find CoreImage on it's own. If not, just supply :core_image as the :processor parameter for has_attachment.
  1. Install MacPorts by following the instructions. A .profile should be created by the install script. It didn't feel like doing that on my system, so I created a .profile file in my home directory with this contents:
    export PATH=/opt/local/bin:/opt/local/sbin:$PATH
    export MANPATH=/opt/local/share/man:$MANPATH
    
  2. Restart Terminal.app
  3. Update the MacPorts ports:
    sudo port selfupdate
  4. Install FreeImage:
    sudo port install freeimage
  5. Install RubyInline:
    sudo gem install RubyInline
  6. Install the ImageScience gem:
    sudo gem install -y image_science
  7. Finally install the attachment_fu plugin (run this in your Rails project directory):
    script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/
That's it. Thanks to Mike Clark for his informative blog post. What next? Read above mentioned informative blog post to learn how to use attachment_fu in your app or read this Advanced Rails Recipe (PDF) on the topic.

Q&A

Q: Why did you use attachment_fu and not one of the other file upload plugins? A: Because (1) Rick Olsen knows what he is doing, (2) is a Rails core contributor and (3) has a proven record of great plugins. Oh, and because attachment_fu seems to (4) be actively maintained and (5) supports file system, database and even Amazon S3 data stores. Q: Why did you choose FreeImage and ImageScience and not ImageMagick and RMagick or MiniMagick? A: Especially RMagick has the reputation of being a leaking memory hog. I haven't tried it myself, but ImageScience and FreeImage seem to be a lean choice and they do a great job. If this was useful for you, please take a minute and recommend me:
Recommend Me
Thank you!

Comments

Morten K. Holst said...

Johannes, a HUGE thanks from here.. Everything is working like a charm now!

This is the kind of stuff RoR-beginners need to get going.

June 30, 2008 08:15 PM

Johannes Fahrenkrug said...

Hi Richard,

Thanks for your comment. I don't know enough about your environment to pinpoint what your problem might be, so I'll just tell you something you can try to make sure your filters really work:
Add "rezize_to" to your has_attachment call, so it looks something like this:

has_attachment :content_type => :image, :size => 1..5.megabytes, :resize_to => 'x300'

Now upload some big images and check if they get resized. If not, your filter doesn't work. Also note that of course thumbnails are only created for image uploads. Check attachment_fu's README for more details.

The *_url methods are part of the named routes in Rails:
http://wiki.rubyonrails.org/rails/pages/NamedRoutes

Please let me know how it works out!

- Johannes

May 26, 2008 06:17 AM

SnowmanInTheSun said...

I can't seem to get the thumbnail feature working. All my image processors are installed correctly, and i've spent a few hours researching but to no avail. I can save and retrieve files perfectly fine, but for whatever reason a photo_thumb.jpg is never created. Do you have any guesses??

Also i'm unfamiliar with "redirect_to @something_url(@something)' i just used :action => 'show'. Let me know if this would make a difference, and maybe give me a link explaining how this works.
OSX - Leopard
Rails - 2.0.2

May 24, 2008 04:59 AM

Comments

Please keep it clean, everybody. Comments with profanity will be deleted.

blog comments powered by Disqus