Skip to content

Instantly share code, notes, and snippets.

@ardianys
Forked from pch/watermark.rb
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ardianys/2ca22ebd47431b92dd3e to your computer and use it in GitHub Desktop.
Save ardianys/2ca22ebd47431b92dd3e to your computer and use it in GitHub Desktop.
Paperclip 4.1.1 watermark processor for Rails 4 and Ruby 2
class User
has_attached_file :photo,
processors: [:watermark],
styles: {
medium: {
geometry: "300x300>",
watermark_path: "#{Rails.root}/public/images/watermark.png",
auto_orient: false
},
thumb: "100x100>",
}
end
# Rails.root /lib/paperclip_processors/watermark.rb
module Paperclip
class Watermark < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
@watermark_path = options[:watermark_path]
@position = options[:position].nil? ? "SouthEast" : options[:position]
end
def make
src = @file
dst = Tempfile.new([@basename].compact.join("."))
dst.binmode
return super unless @watermark_path
params = "-gravity #{@position} #{transformation_command.join(" ")} #{@watermark_path} :source :dest"
begin
success = Paperclip.run("composite", params, source: "#{File.expand_path(src.path)}[0]", dest: File.expand_path(dst.path))
rescue Cocaine::CommandLineError
raise Paperclip::Error, "There was an error processing the watermark for #{@basename}" if @whiny
end
dst
end
end
end
@rsmithlal
Copy link

You really left out so many lines from this file that are required to get this to work. This is the barest shell of a processor. I forked this and added my changes for the working processor. Can't figure out how to do a pull request from the site though.

@rsmithlal
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment