Search code examples
ruby-on-railsimage-compressionimage-optimizationrefile

Optimise/compress images uploaded with refile


Is there a way to optimise images uploaded with refile? I use a CDN so the files are mostly just created once, but i would like them to be optimised to save bandwidth.

I use Mini Magick for image manipulation, but can this also be used to reduce file size / Optimise?


Solution

  • The underlying tools that handle image manipulation are from MiniMagick. Image manipulation functionality has been extracted out of Refile into a separate gem, refile-mini_magick. You should be able to write your own processor as described in the readme.

    Maybe something like this:

    def quality(percentage)
      manipulate! do |img|
        unless img.quality == percentage
          img.write(current_path) do
            self.quality = percentage
          end
        end
    
        img = yield(img) if block_given?
        img
      end
    end