Skip to content

Instantly share code, notes, and snippets.

@pch
Created January 18, 2011 13:43
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pch/784445 to your computer and use it in GitHub Desktop.
Save pch/784445 to your computer and use it in GitHub Desktop.
Paperclip Watermark processor
class User
has_attached_file :photo,
:processors => [:watermark],
:styles => {
:medium => {
:geometry => "300x300>",
:watermark_path => "#{Rails.root}/public/images/watermark.png"
},
: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 PaperclipCommandLineError
raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny
end
dst
end
end
end
@globalxolutions
Copy link

Thanks It worked for me, after loading the file manually on the model:

class Photo < ActiveRecord::Base
require 'paperclip_processors/watermark'
....

Thanks so much!

@kcollignon
Copy link

I keep getting an error:

NameError (uninitialized constant Paperclip::Watermark::PaperclipCommandLineError):
lib/paperclip/watermark.rb:66:in rescue in make' lib/paperclip/watermark.rb:64:inmake'
app/controllers/assets_controller.rb:10:in `create'

@globalxolutions
Copy link

globalxolutions commented Aug 2, 2012 via email

@ardianys
Copy link

Hi, I have made some revision for Rails 4 and Paperclip 4.1.1
https://gist.github.com/ardianys/2ca22ebd47431b92dd3e/revisions
Thanks for your gist

@marcosmartingm
Copy link

Thanks. I needed change some things, look at transformation_command_fix:

def make
  src = @file
  dst = Tempfile.new([@basename].compact.join("."))
  dst.binmode
  return super unless @watermark_path
  transformation_command_fix=transformation_command.join(" ").sub("-auto-orient ", '')
  params = "-gravity #{@position} #{transformation_command_fix} #{@watermark_path} :source :dest"
  Paperclip.run("composite", params, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
  dst
end

@1990prashant
Copy link

Hey, I want to add watermark diagonally can anybody help me what I need to do?

Thanks in advanse.

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