Resize existing images to new style in paperclip & RMagick

我与影子孤独终老i 提交于 2019-12-20 08:01:46

问题


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.


回答1:


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.




回答2:


try this rake task provided by paperclip

rake paperclip:refresh:missing_styles

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



来源:https://stackoverflow.com/questions/2977234/resize-existing-images-to-new-style-in-paperclip-rmagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!