Tuesday, February 12, 2008

attachment_fu - configurations

In our model, we'll use the has_attachment command to hook into attachment_fu.

example:

has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 500.kilobytes,
:resize_to => '320x200>',
:thumbnails => { :thumb => '100x100>' }
:processor => 'Rmagick'

validates_as_attachment

Let's look at each option in detail

for storage, you have three options, :db_system (default), :file_system, or :S3. If you are using :db_system, attachment_fu will convert and save the image to your db as a BLOB (binary large object). If :file_system is used, the file will be saved to your hard disk. If :S3 is used, image will be saved to Amazon's S3 server.

:max_size lets you specific the maximum size of the photo, and there's also a :min_size which works the other way

:resize_to - resize your image to an acceptable width and height like facebook :) It takes a string called the Geometry String, which has the format:

< width > x < height > + - < x > + - < y > { % @ ! < > }

where any of the field can be omitted.

By default, width and height are the maximum value. If you enter something like '300x500' the image will expand or contract to fit the width and height value while maintaining the aspect ratio of the image.

If you want to enforce the image size to be exactly the size you specify, you can append an exclamation mark in the end like so: '300x500!'

You can also specify either the width '300' or the height 'x500' where the missing parameter will be chosen to maintain the aspect ratio

You can append % to specify percentage width and height ('110%' - increase, '90%' - decrease, '110%x90%' = increase width, decrease, height)

You can use @ to specify the maximum area in pixels of an image (I don't see how this is gonna be useful)

You can use < or > to change the dimensions of the image only if its width or height exceeds the geometry specification. < resizes the image only if both of its dimensions are less than the geometry specification. For example, if you specify '300x500>' and the image size is '250x250', the image size will not change. However if the image is '1000x1000', the it'll be resized to '300x300' and vice versa.

Finally x and y are offsets for width and height. + causes x and y to be measured from the left or top edges and - measures from the right or bottom edges. And they are always measured in pixels.

:thumbnails - a set of thumbnails to generate, specified by a has of filename suffixes and resizing options. You can omitted it if you don't want thumbnails. Generating multiple thumbnails will look something like this

:thumbnails => { :thumb_big => '500x500', :thumb_small => '100x100' }

:thumbnail_class - set what class to use for thumbnails (defaulted to whatever model you are in) However, you can generate a seperate model with seperate set of validations

:processor - 'ImageScience', 'Rmagick', or 'MiniMagick'. I like Rmagick cause it has the most features, but MiniMagick is less of a memory hog.

:path_prefix - Path to store the uploaded files, which defaults to public/your_table_name
If you are using S3 backend, it defaults to just your_table_name

validates_as_attachment does all the validations for you, so you have nothing to worry about :)


J

No comments: