Search code examples
carrierwaveminimagick

Having trouble getting multiple carrierwave processes to run


I am attempting to optimize images when resizing them with Carrierwave but haven't had any luck getting this to work. Carrierwave is generating the different sized versions, but my custom optimize and the convert processes aren't running. I've tried calling the different processes in different places, but nothing seems to work.

Any ideas on what I might be doing wrong? Could it be the same issue here: CarrierWave RMagick - How do I cause manipulate! to be called?

class FooUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  # Attempt #1
  process :convert => 'jpg'
  process :optimize
  version :meow do
    process :resize_to_fill => [700, 400]
  end


  # Attempt #2
  version :meow do
     process :convert => 'jpg', :optimize => nil, :resize_to_fill => [700, 400]
  end

  # Attempt #3
  version :meow do
    process :resize_to_limit => [700, 400]
    process :optimize
    process :convert => 'jpg'
  end

  # Attempt #4
  # Tried switching order around since they are being resized but not converted
  version :meow do
    process :convert => 'jpg'
    process :optimize
    process :resize_to_limit => [700, 400]
  end

  # Optimize method used by all versions
  def optimize
    manipulate! do |img|
      img.strip
      img.combine_options do |c|
        c.quality "96"
        c.depth "24"
        c.interlace "plane"
        #c.interlace "Plane" # Tried both cases, seen examples of both
      end
      img
     end
  end

end

Solution

  • Well that not entirely possible they way you want if you are using carrierwave mini-magick or rmagick helper method

    Solution 1)

    if you get rid of carrierwave helper then perhaps it can work do something like this

       process :custom_processing 
    
       def custom_processing 
         // You have to write you own code for and not CarrierWave helper method to achieve this 
         // write your own code to convert to jpg 
         // write your code to optimize
         // write your code to resize from the converted n optimize image
       end
    

    this way you do all your processing in one shot ,having said but you have to dig in mini_magick/rmagick documentation to know how what method to call for each as you cant rely on carrierwave helper (since they are registered/called via process method and one cannot call call/invoke process inside process )

    so something like this you can't achieve

      def custom_processing
        process :convert => 'jpg'
        process :optimize
        process :resize_to_fit  => [700, 400]
      end
    

    Solution 2)

    With Carrierwave Helper but through multiple versions you can eventually achieve this

      version :jpg do
         process :convert => 'jpg'
      end 
    
      version :optimize_jpg ,:from => :jpg do 
        process :optimize
      end 
    
      version :meow,:from => :optimize_jpg do
        process :resize_to_limit => [700, 400]
      end
    

    More reference of this can be found here

    Now this approach take care of headache of Approach 1 digging to documentation(since we are using Carrierwave helper methods), but as you can see you have to create multiple versions to achieve this

    Question : Why Multiple version ?

    Well that how it Carrierwave work it create a uploader for each version and if from options isnt applied it consider the original images (uploaded images) as the source file(parent file) create the desired version