64

I've been using paperclip to upload and auto-resize photos in my Rails app, and I love it. Only problem is about every other month my crazy manager decides he wants a new size to display the photos in. So I add a new style in my Photo model and all is good for new photos, but the pre-existing photos are now a problem. Now that I'm starting to have more than a few photos to deal with I need a programmatic way to resize existing photos. Perhaps there is some paperclip trick for such a thing? I'd really rather not have to figure out RMagick and write a script myself if I don't have to.

tybro0103
  • 48,327
  • 33
  • 144
  • 170

2 Answers2

89

You want the reprocess! method of Paperclip::Attachment. See the docs.

class User < ActiveRecord::Base
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end

# Console...
>> User.find_each { |u| u.avatar.reprocess! }

Also, according to the comments before the reprocess! method, there's a paperclip:refresh Rake task, which might be easier.

Robert Speicher
  • 15,382
  • 6
  • 40
  • 45
  • 5
    Here's the documentation to regenerate your paperclip styles. https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation The command should be `rake paperclip:refresh:YOUR_STYLE_HERE CLASS=User` – Hengjie Feb 21 '13 at 00:18
  • FYI, if you have a polymorphic model to handle all your Paperclip attachments, called `Picture` for example, you'll need to do something like: `Gallery.pictures.file.reprocess!`. – Joshua Pinter May 30 '15 at 23:05
  • This worked for me, whereas the rake task failed (couldn't find the paperclip yml file). I inherited this app, and it doesn't appear to use the yml file. – Dogweather Jul 29 '18 at 02:32
31

try this rake task provided by paperclip

rake paperclip:refresh:missing_styles

Ref: https://github.com/thoughtbot/paperclip#post-processing

Naveed
  • 11,057
  • 2
  • 44
  • 63
  • 6
    ...or do it for just one model: `rake paperclip:refresh:missing_styles CLASS=YourModelName` – Arcolye Oct 31 '13 at 03:33
  • 1
    Dont forget to set the rails environment with the task. `rake paperclip:refresh:missing_styles CLASS=YourModelName RAILS_ENV=production` – Pramod Solanky Jan 05 '16 at 14:56