Skip to content
This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
norman edited this page Sep 12, 2010 · 5 revisions

HasImage 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.

HasImage 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 

Compared to attachment_fu, HasImage has advantages and disadvantages.

Advantages:

  • Simpler, smaller, more easily hackable codebase – and specialized for images only.
  • Installable via Ruby Gems. This makes version dependencies easy when using Rails 2.1.
  • Creates only one database record per image.
  • Has built-in facilities for making distortion-free, fixed-size thumbnails.
  • Doesn‘t regenerate the thumbnails every time you save your model. This means you can easily use it, for example, inside a Member model to store member avatars.

Disadvantages:

  • Doesn‘t save image dimensions. However, if you‘re using fixed-sized images, this is not a problem because you can just read the size from MyModel.thumbnails[:my_size]
  • No support for AWS or DBFile storage, only filesystem.
  • Only supports MiniMagick as an image processor, no RMagick, GD, CoreImage, etc.
  • No support for anything other than image attachments.
  • Not as popular as attachment_fu, which means fewer bug reports, and probably more bugs. Use at your own risk!

Methods

Public Class methods



If you‘re invoking this method, you need to pass in the class for which you want to get default options; this is used to determine the path where the images will be stored in the file system. Take a look at HasImage::ClassMethods#has_image to see examples of how to set the options in your model.

This method is called by your model when you call has_image. It‘s placed here rather than in the model‘s class methods to make it easier to access for testing. Unless you‘re working on the code, it‘s unlikely you‘ll ever need to invoke this method.

  • :resize_to => "200×200",
  • :thumbnails => {},
  • :max_size => 12.megabytes,
  • :min_size => 4.kilobytes,
  • :path_prefix => klass.to_s.tableize,
  • :base_path => File.join(RAILS_ROOT, ‘public’),
  • :convert_to => "JPEG",
  • :output_quality => "85",
  • :invalid_image_message => "Can‘t process the image.",
  • :image_too_small_message => "The image is too small.",
  • :image_too_big_message => "The image is too big.",

[Source]

 # File lib/has_image.rb, line 108 108: def default_options_for(klass) 109: { 110: :resize_to => "200×200", 111: :thumbnails => {}, 112: :max_size => 12.megabytes, 113: :min_size => 4.kilobytes, 114: :path_prefix => klass.to_s.tableize, 115: :base_path => File.join(RAILS_ROOT, ‘public’), 116: :convert_to => "JPEG", 117: :output_quality => "85", 118: :invalid_image_message => "Can’t process the image.", 119: :image_too_small_message => "The image is too small.", 120: :image_too_big_message => "The image is too big." 121: } 122: end 
Clone this wiki locally