<?xml version="1.0" encoding="UTF-8"?>
<page>
  <body>&quot;API docs&quot;:http://sargon.interinter.net/acts_as_image/

ActsAsImage is a Rails plug-in that scales images and saves them to disk. It uses &quot;RMagick&quot;:http://rmagick.rubyforge.org/, so it can handle all the image formats ImageMagick can. Images are saved as either JPEG or GIF, depending on if it's an animation or not.

I've intentionally kept it very simple. If you want something a bit more feature-rich, other plug-ins such as Rick Olson's &quot;attachment_fu&quot;:http://weblog.techno-weenie.net/2007/2/25/attachment_fu-tutorial are probably more to your liking.

h3. Installing

Download the plugin from &quot;Github&quot;:https://github.com/toretore/acts_as_image/tree and put it in the @vendor/plugins@ directory.

h3. Model

If you haven't got a model to represent your images yet, ActsAsImage can generate one for you, along with the necessary migration, that's set up to use the plug-in.

[terminal]
script/generate acts_as_image model Image
rake db:migrate
[/terminal]

If you want to use an existing model, make sure the database table has the fields @content_type@, @hash_string@ and @original_filename@.

The model will look something like this:

[ruby]
class Image &lt; ActiveRecord::Base

  acts_as_image #Must come first

  self.image_sizes = {
    :original =&gt; '100%x100%',
    :large =&gt; '&gt;800x600',
    :medium =&gt; '&gt;640x480',
    :small =&gt; '&gt;320x200',
    :thumb =&gt; ['100x100!', :crop]
  }

  self.image_save_path = File.join(RAILS_ROOT, 'public', 'images', 'uploads')
  self.image_read_path = ['images', 'uploads'].join('/')

end
[/ruby]

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 &quot;geometry string&quot;:http://www.simplesystems.org/RMagick/doc/imusage.html#geometry.

h3. Using it

To save images with this model, you attach the uploaded file to the model's virtual @file@ attribute:

[ruby]
class PicturesController

  def create
    image = Image.new
    image.file = params[:image][:file]
    image.save
  end

end
[/ruby]

You can now access the image's URL via the @url@ method:

[html]
  &lt;%= image_tag(image.url('small'), :alt =&gt; h(image.title)) %&gt;
[/html]

h3. Other stuff

To keep this page uncluttered, more information on how to customise how the plug-in works has been moved into sub-pages of this page (links at the top).</body>
  <created-at type="datetime">2007-04-13T23:12:58-04:00</created-at>
  <id type="integer">2</id>
  <parent-id type="integer" nil="true"></parent-id>
  <published type="boolean">true</published>
  <slug>acts_as_image</slug>
  <summary></summary>
  <title>ActsAsImage</title>
  <updated-at type="datetime">2008-04-17T10:07:06-04:00</updated-at>
</page>
