Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reprocessing images of different versions in Carrierwave

Using Carrierwave, I created 3 versions of an avatar - an original, a small_thumb and a large_thumb using the following lines:

process :resize_to_limit => [400, 400]  

   version :big_thumb do
     process :resize_to_limit => [80, 80]
   end

   version :small_thumb do
     process :resize_to_limit => [50, 50]
   end

I added an additional method in my AvatarUploader class:

def reprocess(x,y,w,h)
        manipulate! do |img|
            img.crop(x.to_i, y.to_i, w.to_i, h.to_i, true) 

            end
resize_to_limit(180,180)  
end

which is called in my model after an update is performed:

attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :reprocess_image, :if => :cropping?

def cropping?
    !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end

private

def reprocess_image
image.reprocess(crop_x,crop_y,crop_w,crop_h)

end

I have managed to crop and resize the original version, but I can't seem to update the 2 thumbnails along with it. I tried a few different techniques to no avail.

Any suggestions?

like image 566
Jonathan Chiu Avatar asked Feb 27 '11 09:02

Jonathan Chiu


2 Answers

Try

image.recreate_versions!

Sorry, on the bus. I can't expound on that.

like image 119
Michael Reed Avatar answered Nov 09 '22 23:11

Michael Reed


You need to call image.cache_stored_file! before calling recreate_versions!

It's weird because the method itself calls that if the file is cached, but for some reason it wasn't working.

So that would be something like:

def reprocess_image
  image.reprocess(crop_x, crop_y, crop_w, crop_h)
  image.cache_stored_file!
  image.recreate_versions!
end
like image 44
edgarjs Avatar answered Nov 10 '22 01:11

edgarjs