yurko
3/14/2017 - 3:24 PM

carrierwave mini_magick image processing - quality, strip, exif rotation, gaussian blur, interlace

carrierwave mini_magick image processing - quality, strip, exif rotation, gaussian blur, interlace

  # fix exif rotation before strip the exif data
  process :fix_exif_rotation
  process :strip
  
  # set gaussion blur to optimize the image
  process :gaussian_blur => 0.05
  
  # default quality is 85
  # process :quality => 85 # Percentage from 0 - 100
  
  # set the interlace to plane for progressive jpeg
  # this might increase the size of images a bit
  process :interlace => Plane 

  # Create 820x820 fill image size
  version :medium do
    process :resize_to_limit => [820, 820]
    process :interlace => Plane 
  # Create 380x380 fill image size
  end
  version :small do
    process :resize_to_limit => [380, 380]
    process :interlace => Plane 
  end
#config/initializers/carrierwave.rb
module CarrierWave
  module MiniMagick
		# Rotates the image based on the EXIF Orientation
    def exif_rotation
      manipulate! do |img|
        img.auto_orient
        img = yield(img) if block_given?
        img
      end
    end
 
    # Strips out all embedded information from the image
    def strip
      manipulate! do |img|
        img.strip
        img = yield(img) if block_given?
        img
      end
    end
    # Tiny gaussian blur to optimize the size
    def gaussian_blur(radius)
      manipulate! do |img|
        img.gaussian_blur(radius.to_s)
        img = yield(img) if block_given?
        img
      end
    end
    
    # set the Interlace of the image plane/basic
    def interlace(type)
      manipulate! do |img|
        img.interlace(type.to_s)
        img = yield(img) if block_given?
        img
      end
    end
 
    # Reduces the quality of the image to the percentage given
    def quality(percentage)
      manipulate! do |img|
        img.quality(percentage.to_s)
        img = yield(img) if block_given?
        img
      end
    end

  end
end