Friday, August 12, 2011

Paperclip Resizing Options

Paperclip is intended as an easy file attachment library for ActiveRecord. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes as possible. This means they aren't saved to their final locations on disk, nor are they deleted if set to nil, until ActiveRecord::Base#save is called. It manages validations based on size and presence, if required. It can transform its assigned image into thumbnails if needed, and the prerequisites are as simple as installing ImageMagick (which, for most modern Unix-based systems, is as easy as installing the right packages). Attached files are saved to the filesystem and referenced in the browser by an easily understandable specification, which has sensible and useful defaults.
You can see the Github Repository through the link below

Paperclip GitHub Page


Resizing Options

The link below give us a lot of technical info about the options for resizing through ImageMagick

Complete list of ImageMagick options


But how can we use this options through Paperclip?

Paperclip has a wiki page giving us a few examples about how-to use it

https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation

For example:

Having a user model with an atached photo with some thumbnails (thumb, small, medium and others); I can resize it's dimensions using flags like "#", ">" or "^" and others

But how can we apply this to our styles?

We can use this for:

- fixed width and/or height

It can be used like below:
...
has_attached_file :photo,
  :styles => {
  :tiny => "100x100",            # fixed width and height
  :thumb => "100>x100", # resize to a fixed width if original height if greather than specified
                                                   # dimension and fixed height
  :small => "200x200>",   # fixed width and resize to a fixed height if original height i greather than
                                                   # specified dimension
  :medium => "200 "200x200<" # fixed width and resize to a fixed heigth if original height i less
                                                            # than specified dimension
}
...

- fixed width and/or proportional height It can be used like below:
...
has_attached_file :photo, :styles => {
  :small => "100",       # fixed width and proportional height
  :medium => "x100" # proportional width and fixed height
}
...

For more details about the geometry argument ("widthxheigth - 100x100") and these options see the link below

http://www.imagemagick.org/script/command-line-processing.php#geometry

No comments:

Post a Comment