Class: Paperclip::Watermark
- Inherits:
-
Processor
- Object
- Processor
- Paperclip::Watermark
- Defined in:
- lib/paperclip/watermark.rb
Instance Method Summary collapse
- #composition_options(destination_path) ⇒ Object
-
#initialize(file, options = {}, attachment = nil) ⇒ Watermark
constructor
A new instance of Watermark.
- #make ⇒ Object
- #scaled_size_for_watermark ⇒ Object
- #watermak_position ⇒ Object
- #watermark_command ⇒ Object
Constructor Details
#initialize(file, options = {}, attachment = nil) ⇒ Watermark
Returns a new instance of Watermark.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/paperclip/watermark.rb', line 6 def initialize(file, = {}, = nil) super @file = file @whiny = .fetch(:whiny, true) @watermark_path = [:watermark_path] @dissolve = .fetch(:watermark_dissolve, 30) @position = [:watermark_position] @distance_from_top = [:watermark_distance_from_top] raise Paperclip::Error.new('Position or distance from top required') if @position.nil? && @distance_from_top.nil? raise Paperclip::Error.new('Missing watermark path') if @watermark_path.nil? end |
Instance Method Details
#composition_options(destination_path) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/paperclip/watermark.rb', line 29 def (destination_path) = [] << "-dissolve #{@dissolve}%" << '-quality 100' << watermak_position << watermark_command << @file.path << destination_path .join(' ') end |
#make ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/paperclip/watermark.rb', line 19 def make destination = Tempfile.new([file_basename, current_format ? ".#{current_format}" : '']) destination.binmode Paperclip.run('composite', (destination.path)) destination rescue Paperclip::Errors::CommandNotFoundError raise Paperclip::Errors::CommandNotFoundError, "There was an error processing the watermark for #{@file.path}" if @whiny end |
#scaled_size_for_watermark ⇒ Object
58 59 60 61 62 |
# File 'lib/paperclip/watermark.rb', line 58 def scaled_size_for_watermark destination_width = file_size.width - 20 calculated_height = watermark_size.height.to_f / watermark_size.width.to_f * destination_width.to_f Paperclip::Geometry.new(destination_width, calculated_height.to_i) end |
#watermak_position ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/paperclip/watermark.rb', line 40 def watermak_position if @position "-gravity #{@position}" else "-geometry +#{calculated_watermak_position.first}+#{calculated_watermak_position.last}" end end |
#watermark_command ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/paperclip/watermark.rb', line 48 def watermark_command = [] << '\\(' << @watermark_path << '-resize' << "#{scaled_size_for_watermark.width}x#{scaled_size_for_watermark.height}" << '\\)' .join(' ') end |