Reprocessing images in Carrierwave
Asked Answered
R

2

31

Let's say my model has an image with :thumb and the client wants :tiny and :nano thumbnails.

How do I reprocess all the existing images using a rake task?

I've found a rake task that I thought would do it https://gist.github.com/777788 but it's giving me errors.

Ringler answered 30/1, 2012 at 14:51 Comment(0)
C
57

According to the Carrerwave documentation you can use following commands:

Model.all.each do |model|
  model.image.recreate_versions!
end
Credenza answered 30/1, 2012 at 14:56 Comment(7)
I've put that in a rake task now, it's a bit hard coded to my app but it'll do the trickRingler
will this recreate EVERYTHING, or just the newer versions?Cristiecristin
It will recreate everything, if you need to recreate let's say images for last 100 records -> Model.last(100).each { |m| m.image.recreate_versions! }Credenza
Depending on the storage, you may need to do this: github.com/carrierwaveuploader/carrierwave/wiki/…Allegro
To get this to work, I had to add model.save inside the loop (which is also done by the code @Allegro pointed to).Quartus
@Quartus So true. At least on some versions of carrierwave or it might be Mongoid related, calling recreate_versions! does reprocess the images, but when you reload the model it references the old updated to reference these new files if you do not explicitly call save on the model.Sole
I added the check: model.image.recreate_versions! if model.image? since not all objects have uploaded images in my case.Ymir
D
2

I wanted to expand on this great answer by Mikhail Nikalyukin

To Reprocess a single version you can do something like this

Model.all.each do |model|
  model.image.recreate_versions!(:version1, :version2)
end

this way if you added a new version you dont have to do all of them again

Demineralize answered 29/5, 2017 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.