17

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?

Jonathan Chiu
  • 1,637
  • 2
  • 16
  • 25

5 Answers5

55

Try

image.recreate_versions!

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

tybro0103
  • 48,327
  • 33
  • 144
  • 170
Michael Reed
  • 2,362
  • 23
  • 15
  • 14
    `Model.all.each {|m| m.avatar.recreate_versions!}` Will work great if you have added a new version to a model with pre-existing records. – tybro0103 Oct 31 '12 at 16:07
  • You may also need to call `save` on the model. At least I did. Might depend on your application configuration... so something like this worked for me: `Model.all.each {|m| m.avatar.recreate_versions!; m.save! }` – Timo Apr 04 '14 at 13:17
  • no need to apologize. just what I needed. bus coding a-ok. – Andrew Arrow Feb 03 '16 at 06:12
3

This HowTo was most helpful for me (even if I don't use fog):

https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Recreate-and-reprocess-your-files-stored-on-fog

I added a reprocess method on my model and then called it in for each loop in my rake task:

  def reprocess
    begin
      self.process_photo_upload = true
      self.photo.cache_stored_file!
      self.photo.retrieve_from_cache!(photo.cache_name)
      self.photo.recreate_versions!
      self.save!
    rescue => e
      STDERR.puts  "ERROR: MyModel: #{id} -> #{e.to_s}"
    end
  end

Rake:

task :reprocess_photos => :environment do MyModel.all.each{|mm| mm.reprocess} end

PS: Rails 4.2

3

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
edgarjs
  • 506
  • 2
  • 9
  • this didn't work for me. is it possible because I'm storing the images on SC3 with fog? – kibaekr Jan 18 '13 at 08:40
  • @kibaekr right, you'll need to download the image, reprocess it and then reupload it. – edgarjs Jan 18 '13 at 17:50
  • 1
    for me doing something like this worked: "image.recreate_versions! unless image.blank?" inside the loop. I didn't have to download the images, I just did it in the console – kibaekr Jan 25 '13 at 17:44
1

I haven't tried but maybe putting something like.

def reprocess_image
  image.reprocess(crop_x,crop_y,crop_w,crop_h)
  image.recreate_versions!
end
1

check this latest RailsCast:

http://railscasts.com/episodes/182-cropping-images-revised

after cropping one version of the image, you can then either calculate the cropping coordinates for the other versions, or probably easier, scale down the cropped image with the same aspect ratios as the other versions of the original image

Tilo
  • 33,354
  • 5
  • 79
  • 106