Last active
December 19, 2015 07:39
-
-
Save sao/5920217 to your computer and use it in GitHub Desktop.
Masker: Custom image processor for Paperclip (requires masking image)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Paperclip | |
class Masker < Processor | |
def initialize file, options = {}, attachment = nil | |
super | |
@format = File.extname(@file.path) | |
@basename = File.basename(@file.path, @format) | |
end | |
def make | |
source = @file | |
destination = Tempfile.new([@basename, '.png']) | |
destination.binmode | |
parameters = [ | |
':source', ':mask', | |
'-alpha', 'off', | |
'-gravity', 'center', | |
'-compose', 'CopyOpacity', | |
'-composite', '-trim', | |
':dest' | |
].join(" ") | |
begin | |
mask_path = File.expand_path('lib/paperclip_processors/mask.gif') | |
Paperclip.run("convert", parameters, :source => File.expand_path(source.path), :mask => mask_path, :dest => File.expand_path(destination.path)) | |
rescue PaperclipCommandLineError | |
raise PaperclipError, "There was an error during the mask for #{@basename}" if @whiny | |
end | |
destination | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment