Friday, August 10, 2007

RMagick

This library makes handling of uploaded files in Ruby on Rails as easy as it should be. It helps you to not repeat yourself and write the same file handling code all over the place while providing you with nice features like keeping uploads during form redisplays, nice looking URLs for your uploaded files and easy integration with RMagick to resize uploaded images and create thumb-nails. Files are stored in the filesystem and the filename in the database.

Example

As you can judge a library best by looking at how to use it, here is a short example:

Just make the "image" column ready for handling uploaded files...

     class Entry < ActiveRecord::Base         file_column :image     end      

... generate file fields that keep uploaded images during form redisplays to your view...

     <%= file_column_field "entry", "image" %>      

... and display uploaded images in your view:

     <%= image_tag url_for_file_column("entry", "image") %>      

It's just as easy! Why should it be any more difficult for a Rails application?

So what about the RMagick integration? Have a look:

To resize every uploaded image to a maximum size of 640x480, you just have to declare an additional option.

     class Entry < ActiveRecord::Base         file_column :image, :magick => { :geometry => "640x480>" }     end      

You can even automatically create versions in different sizes that have nice filenames...

     class Entry < ActiveRecord::Base         file_column :image, :magick => {            :versions => { "thumb" => "50x50", "medium" => "640x480>" }         }     end      

... and display them in your view:

      <%= image_tag url_for_file_column("entry", "image") %>       

No comments:

analytics