Links
HasImage
Categories
HasImage
HasImage is a plugin/gem that allows Ruby on Rails applications to have attached images. It is very small and lightweight: it only requires one column (“has_image_file”) in your model to store the uploaded image’s file name.
It is, by design, very simplistic: It only supports using a filesystem for storage, and only supports MiniMagick as an image processor. However, its code is very small, clean and hackable, so adding support for other backends or processors should be fairly easy.
HasImage works best for sites that want to show image galleries with fixed-size thumbnails. It uses ImageMagick’s crop and center gravity functions to produce thumbnails that generally look acceptable, unless the image is a panorama, or the subject matter is close to one of the margins, etc. For most sites where people upload pictures of themselves or their pets the generated thumbnails will look good almost all the time.
It’s pretty easy to change the image processing / resizing code; you can just override HasImage::Processor#resize_image to do what you wish:
module HasImage::
class Processor
def resize_image(size)
@image.combine_options do |commands|
commands.my_custom_resizing_goes_here
end
end
end
end
Another image attachment library? Why?
The three chief virtues of a programmer are: Laziness, Impatience and Hubris. – Larry Wall
Attachment_fu is too large and general for some of the places I want to use images. I sometimes found myself writing more code to hack attachment_fu than it took to create this gem. In fact, most of the code here has been plucked from my various projects that use attachment_fu.
The other image attachment libraries I found fell short of my needs for various other reasons, so I decided to roll my own.
Examples
Point-and-drool use case:
It’s probably not what you want, but it may be useful for bootstrapping.
class Member < ActiveRecord::Base
has_image
end
Single image, no thumbnails, with some size limits:
class Picture < ActiveRecord::Base
has_image :resize_to => "200x200",
:max_size => 3.megabytes,
:min_size => 4.kilobytes
end
Image with some thumbnails:
class Photo < ActiveRecord::Base
has_image :resize_to => "640x480",
:thumbnails => {
:square => "200x200",
:medium => "320x240"
},
:max_size => 3.megabytes,
:min_size => 4.kilobytes
end
HasImage also provides a view helper to make displaying the images extremely simple:
<%= image_tag_for(@photo, :thumb => :square) %>
Getting it
Has image can be installed as a gem, or as a Rails plugin. Gem installation is easiest, and recommended:
gem install norman-has_image—source http://gems.github.com
and add
require ‘has_image’
to your environment.rb file.
Alternatively, you can install it as a Rails plugin:
./script plugin install git://github.com/norman/has_image.git
Rails versions before 2.1 do not support plugin installation using Git, so if you’re on 2.0 (or earlier), then please install the gem rather than the plugin.
Then, make sure the model has a column named “has_image_file.”
Git repository:
git://github.com/norman/has_image.git
Hacking it
Don’t like the way it makes images? Want to pipe the images through some crazy fast seam carving library written in OCaml, or watermark them with your corporate logo? Happiness is just a monkey-patch away:
module HasImage
class Processor
def resize_image(size)
# your new-and-improved thumbnailer code goes here.
end
end
end
HasImage follows a philosophy of “skinny model, fat plugin.” This means that it tries to pollute your ActiveRecord model with as little functionality as possible, so that in a sense, the model is acts like a “controller” and the plugin like a “model” as regards the image handling functionality. This makes it easier to test, hack, and reuse, because the storage and processing functionality is largely independent of your model, and of Rails.
My goal for HasImage is to keep it very small. If you need a lot of functionality that’s not here, instead of patching this code, you will likely be better off using attachment_fu, which is much more powerful, but also more complex.
Bugs
Please report them on Lighthouse.
At the time of writing (July 2008), HasImage is in its infancy. Your patches, bug reports and withering criticism are more than welcome.
Vitals
| Home | http://randomba.org/ |
|---|---|
| Repository | git://github.com/norman/has_image.git |
| License | Rails' (MIT) |
| Tags |
attachments images upload
|
| Rating | (4 votes) |
| Owner | Norman Clarke |
| Created | 25 July 2008 |

