-
Notifications
You must be signed in to change notification settings - Fork 1
Thumbnail Generation
Common use cases for thumbnails are pretty easy with Paperclip. You define your preferred sizes in your initial has_attached_file
call:
class User < ActiveRecord::Base # Paperclip has_attached_file :photo, :styles => { :thumb => "100x100#", :small => "150x150>", :medium => "200x200" } end
Then you use them like this:
<%= image_tag @user.photo.url %> <%= image_tag @user.photo.url(:thumb) %> <%= image_tag @user.photo.url(:small) %>
Default behavior is to resize the image and maintain aspect ratio (i.e. the :medium version of a 300×150 image will be 200×100). Some commonly used options are:
- trailing
#
, thumbnail will be centrally cropped, ensuring the requested dimensions. - trailing
>
, thumbnail will only be modified if it is currently larger requested dimensions. (i.e. the :small thumb for a 120×80 original image will be unchanged)
You can see a complete list of ImageMagick options for more.
You can (re)generate your thumbnails en masse with Paperclip’s rake tasks. Using our example class above:
rake paperclip:refresh:thumbnails CLASS=User
If you are using the gem required version of paperclip the rake tasks may not be auto-loaded, but you can copy them into your lib/tasks
directory if needed.
If you need more manual control or have a lot of thumbnails and only want to process a few, you can use #reprocess!
like so:
users_to_reprocess.each do |user| user.photo.reprocess! end