Acts As Image plugin
Plugin details
Documentation
ruby script/plugin install git://github.com/toretore/acts_as_image.git
Model
===========
If you haven’t created your model yet, you can use the generator to create a model and a migration that can be used with ActsAsImage:
script/generate acts_as_image model Image
Then, migrate to create the new table:
rake db:migrate
If you have an existing model that you want to use, make sure it has the fields content_type, hash_string and original_filename. In addition, the plugin will create an attribute file on the model that will be used to hold the image before it is written to disk. Finally, add acts_as_image to the model class definition.
Controller and view
=======================
You will have to add the uploaded file to the model’s file attribute before saving it. You can have this done automatically by making sure file is accessible to mass assignment and naming the field in the form model_name[file].
Paths and sizes
You can control which path the images will be written to, and what sizes the image will be resized to. Here’s an example model definition:
class Image < ActiveRecord::Base acts_as_image #Must come first self.image_sizes = { :original => '100%x100%', :large => '>800x600', :medium => '>640x480', :small => '>320x200', :thumb => ['100x100!', :crop] } self.image_save_path = File.join(RAILS_ROOT, 'public', 'images', 'uploads') self.image_read_path = ['images', 'uploads'].join('/') end
Image.image_sizes contains a hash with the names and sizes of the images that will be written to disk. The key will be used as the filename, and the value is an RMagick geometry string (www.simplesystems.org/RMagick/doc/imusage.html#geometry). If the value is an array, the first element is the geometry string and the second is either :crop or :scale, specifying if the image should be scaled or cropped (after being resized). The original image won’t be saved by default, but you can specify an :original entry with ‘100%x100%’ to achieve the same result.
Image.image_save_path is the path in which the images will be written on the server. Image.image_read_path is the path to the image that a browser sees.
Each image also has its own sub-path that consists of a MD5 hash that is split up into three parts. This is to avoid hitting file system limits on how many files a directory can contain. So, the full path to an image will look something like public/images/uploads/3/d/35830c5caa0009b1e9b7d51964d280/small.jpg. The path for use in a view can be retrieved via the url method on the image:
<%= image_tag(image.url('small'), :alt => h(image.title)) %>
Further Documentation
There is currently no advanced documentation for this plugin.
New documentationEdit plugin | Back in time (1 older version) | Last edited by: scott, 7 months ago

