Reprocessing images of different versions in Carrierwave

前端 未结 5 1364
甜味超标
甜味超标 2021-02-02 14:12

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 => [40         


        
相关标签:
5条回答
  • 2021-02-02 14:42

    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
    
    0 讨论(0)
  • 2021-02-02 14:46

    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
    
    0 讨论(0)
  • 2021-02-02 14:59

    Try

    image.recreate_versions!
    

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

    0 讨论(0)
  • 2021-02-02 15:02

    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

    0 讨论(0)
  • 2021-02-02 15:05

    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

    0 讨论(0)
提交回复
热议问题