Paperclip::ContentTypeProcessor

Allows to add file processing by content type for paperclip attachments.

Requirements

libmagic

Installation

Add this line to your application's Gemfile:

gem 'paperclip-content_type_processor'

And then execute:

$ bundle

Or install it by yourself as:

$ gem install paperclip-content_type_processor

Usage

  1. Specify file processing per content type:
Paperclip::ContentTypeProcessor.add_content_type_processor("content/type", "command", "command arguments")
  1. Add content type processor for attachment:
has_attached_file :avatar,
    styles: { medium: '96x96#' },
    processors: [:thumbnail, :content_type_processor]
end

Example

It can be used for image compression/optimizations.

  1. Install image optimizers:

  2. Add initializer in your rails app config/initializers/content_type_processor.rb, and specify file processing per content type:

Paperclip::ContentTypeProcessor.add_content_type_processor("image/jpeg", "jpegoptim", "--strip-all --max=90")
Paperclip::ContentTypeProcessor.add_content_type_processor("image/png", "optipng", "-o6 -strip all")
  1. Enable content type processing for user avatars:
class User < ActiveRecord::Base
  has_attached_file :avatar,
    styles: { medium: '96x96#' },
    processors: [:thumbnail, :content_type_processor]
end

So all user avatars will be processed by 'jpegoptim' or 'optipng' according to its content type.

Note! If you want to generate thumbnails, you should also specify :thumbnails in processor list

Contributing

  1. Fork it ( http://github.com/ingeniarius/paperclip-content_type_processor/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request