module FileColumn::MagickExtension
If you are using file_column to upload images, you can directly process the images with RMagick, a ruby extension for accessing the popular imagemagick libraries. You can find more information about RMagick at rmagick.rubyforge.org.
You can control what to do by adding a :magick
option to your options hash. All operations are performed immediately after a new file is assigned to the file_column attribute (i.e., when a new file has been uploaded).
Resizing images¶ ↑
To resize the uploaded image according to an imagemagick geometry string, just use the :size
option:
file_column :image, :magick => {:size => "800x600>"}
If the uploaded file cannot be loaded by RMagick, file_column will signal a validation error for the corresponding attribute. If you want to allow non-image files to be uploaded in a column that uses the :magick
option, you can set the :image_required
attribute to false
:
file_column :image, :magick => {:size => "800x600>", :image_required => false }
Multiple versions¶ ↑
You can also create additional versions of your image, for example thumb-nails, like this:
file_column :image, :magick => {:versions => { :thumb => {:size => "50x50"}, :medium => {:size => "640x480>"} }
These versions will be stored in separate sub-directories, named like the symbol you used to identify the version. So in the previous example, the image versions will be stored in “thumb”, “screen” and “widescreen” directories, resp. A name different from the symbol can be set via the :name
option.
These versions can be accessed via FileColumnHelper’s url_for_image_column
method like this:
<%= url_for_image_column "entry", "image", :thumb %>
Cropping images¶ ↑
If you wish to crop your images with a size ratio before scaling them according to your version geometry, you can use the :crop directive.
file_column :image, :magick => {:versions => { :square => {:crop => "1:1", :size => "50x50", :name => "thumb"}, :screen => {:crop => "4:3", :size => "640x480>"}, :widescreen => {:crop => "16:9", :size => "640x360!"}, } }
Custom attributes¶ ↑
To change some of the image properties like compression level before they are saved you can set the :attributes
option. For a list of available attributes go to www.simplesystems.org/RMagick/doc/info.html
file_column :image, :magick => { :attributes => { :quality => 30 } }
Custom transformations¶ ↑
To perform custom transformations on uploaded images, you can pass a callback to file_column:
file_column :image, :magick => Proc.new { |image| image.quantize(256, Magick::GRAYColorspace) }
The callback you give, receives one argument, which is an instance of Magick::Image, the RMagick image class. It should return a transformed image. Instead of passing a Proc
object, you can also give a Symbol
, the name of an instance method of your model.
Custom transformations can be combined via the standard :size and :crop features, by using the :transformation option:
file_column :image, :magick => { :transformation => Proc.new { |image| ... }, :size => "640x480" }
In this case, the standard resizing operations will be performed after the custom transformation.
Of course, custom transformations can be used in versions, as well.
Note: You’ll need the RMagick extension being installed in order to use file_column’s imagemagick integration.
Public Class Methods
# File lib/magick_file_column.rb, line 241 def self.process_options(options,create_name=true) case options when String then options = {:size => options} when Proc, Symbol then options = {:transformation => options } end if options[:geometry] options[:size] = options.delete(:geometry) end options[:image_required] = true unless options.key?(:image_required) if options[:name].nil? and create_name if create_name == true hash = 0 for key in [:size, :crop] hash = hash ^ options[key].hash if options[key] end options[:name] = hash.abs.to_s(36) else options[:name] = create_name end end options end