Skip to content

Instantly share code, notes, and snippets.

@sao
Last active December 19, 2015 07:39
Show Gist options
  • Save sao/5920217 to your computer and use it in GitHub Desktop.
Save sao/5920217 to your computer and use it in GitHub Desktop.
Masker: Custom image processor for Paperclip (requires masking image)
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