Module: HasImage

Defined in:
lib/has_image.rb,
lib/has_image/storage.rb,
lib/has_image/processor.rb,
lib/has_image/view_helpers.rb

Overview

HasImage

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

  • 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!

Defined Under Namespace

Modules: ClassMethods, ModelClassMethods, ModelInstanceMethods, ViewHelpers Classes: FileTooBigError, FileTooSmallError, InvalidGeometryError, Processor, ProcessorError, Storage, StorageError

Class Method Summary collapse

Class Method Details

.default_options_for(klass) ⇒ Object

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 => “200x200”,

  • :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.”,



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/has_image.rb', line 109

def default_options_for(klass)
  {
    :resize_to => "200x200",
    :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."
  }
end

.enableObject

Enables has_image functionality. You probably don’t need to ever invoke this.



80
81
82
83
84
85
# File 'lib/has_image.rb', line 80

def enable # :nodoc:
  return if ActiveRecord::Base.respond_to? :has_image
  ActiveRecord::Base.send(:include, HasImage)
  return if ActionView::Base.respond_to? :image_tag_for
  ActionView::Base.send(:include, ViewHelpers)
end

.included(base) ⇒ Object

:nodoc:



74
75
76
# File 'lib/has_image.rb', line 74

def included(base) # :nodoc:
  base.extend(ClassMethods)
end