No release in over 3 years
Low commit activity in last 3 years
JPEG and PNG compression for Paperclip gem
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
~> 12.3, >= 12.3.3
= 3.7.0

Runtime

>= 6.4.1
~> 1.0, >= 1.0.0
 Project Readme

kt-paperclip-compression

JPEG and PNG compression processor for kt-paperclip. Under the hood, jpegtran and optipng libraries are used.

This fork of the original paperclip-compression gem works with kt-paperclip.

Installation

Add to your Gemfile.

gem 'kt-paperclip-compression'

Usage

This is the basic usage. This will compress both JPEG and PNG files with the default options.

class User < ActiveRecord::Base
  has_attached_file :avatar,
    styles: { medium: '300x300>', thumb: '100x100>' },
    processors: [:thumbnail, :compression]
end

Disable PNG compression and change default options for JPEG compression for thumb.

class User < ActiveRecord::Base
  has_attached_file :avatar,
    styles: {
      medium: '300x300>',
      thumb: {
        geometry: '100x100>',
        processor_options: {
          compression: {
            png: false,
            jpeg: '-copy none -optimize'
          }
        }
      }
    },
    processors: [:thumbnail, :compression]
end

kt-paperclip-compression uses binaries which are bundled with the gem. So you don't need to install anything. But if these binaries don't work for you, you can use your own.

class User < ActiveRecord::Base
  has_attached_file :avatar,
    styles: {
      thumb: {
        geometry: '100x100>',
        processor_options: {
          compression: {
            jpeg: {
              command: '/path/to/jpegtran',
              options: '-copy none -optimize'
            }
          }
        }
      }
    },
    processors: [:thumbnail, :compression]
end

Defaults

Default options for jpegtran is -copy none -optimize -perfect and default options for optipng is -o 5 -quiet.

You can use kt-paperclip's default options to define global defaults for all your paperclip attachments. Use compression key.

Example for config/application.rb:

module YourApp
  class Application < Rails::Application
    # Other code...

    config.paperclip_defaults = { :compression => { :png => false, :jpeg => '-optimize' } }
  end
end

Example for Rails initializer:

Paperclip::Attachment.default_options[:compression] = { :png => false, :jpeg => '-optimize' }

For more information about paperclip defaults: https://github.com/thoughtbot/paperclip#defaults

License

kt-paperclip-compression is released under the MIT License.