It handles image upload, resize, generate thumbnails, storing image info to database
and saving the file to physical disk drive
to install attachment_fu, we type
~$ .script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/
if you don't have imagemagick and rmagick installed
~$ sudo apt-get install imagemagick
~$ dpkg -l | grep magick
~$ sudo apt-get install libmagick9-dev
~$ sudo gem install rmagick -v=1.15.12
window users: you can go download the imagemagick executable
and install rmagick by doing gem install rmagick -v=1.15.12
now let's generate a model to test it out
~$ .script/generate model photo
open up the migration file and enter the following
class CreatePhotos < ActiveRecord::Migration
def self.up create_table :photos do |t|
t.column :user_id, :integer
t.column :parent_id, :integer
t.column :content_type, :string
t.column :filename, :string
t.column :thumbnail, :string
t.column :size, :integer
t.column :width, :integer
t.column :height, :integer
end
end
def self.down
drop_table :photos end
end
:user_id - I'm assuming these photos belong to someone, you can replace it with another model_id depending on the relationship
:parent_id - don't touch, it's reserved for attachment_fu
:content_type - specify the content type of your data, default is image, but it can also be audio, or video the rest are pretty self explanatory, and we'll revisit these fields in a moment. now save your file, and do a quick rake db:migrate
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:
\
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 your specify, you can append an exclamation mark in the end '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 maxium area in pixels of an image (I don't see how this is gonna be useful)
You can use > or <>' 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. That took long enough, let's move on
: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.
: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 :)
This post is getting way too long. I'll have a seperate post for view and controller
J
No comments:
Post a Comment