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
- Specify file processing per content type:
Paperclip::ContentTypeProcessor.add_content_type_processor("content/type", "command", "command arguments")
- 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.
-
Install image optimizers:
- jpegoptim (http://freecode.com/projects/jpegoptim)
- optipng (http://optipng.sourceforge.net/)
-
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")
- 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
- Fork it ( http://github.com/ingeniarius/paperclip-content_type_processor/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request