Class: Paperclip::Watermark
- Inherits:
-
Processor
- Object
- Processor
- Paperclip::Watermark
- Defined in:
- lib/paperclip-watermark/watermark.rb
Instance Attribute Summary collapse
-
#convert_options ⇒ Object
Handles watermarking of images that are uploaded.
-
#current_geometry ⇒ Object
Handles watermarking of images that are uploaded.
-
#dissolve_option ⇒ Object
Handles watermarking of images that are uploaded.
-
#format ⇒ Object
Handles watermarking of images that are uploaded.
-
#overlay ⇒ Object
Handles watermarking of images that are uploaded.
-
#position ⇒ Object
Handles watermarking of images that are uploaded.
-
#target_geometry ⇒ Object
Handles watermarking of images that are uploaded.
-
#tile ⇒ Object
Handles watermarking of images that are uploaded.
-
#watermark_geometry ⇒ Object
Handles watermarking of images that are uploaded.
-
#watermark_option ⇒ Object
Handles watermarking of images that are uploaded.
-
#watermark_path ⇒ Object
Handles watermarking of images that are uploaded.
-
#watermark_resize ⇒ Object
Handles watermarking of images that are uploaded.
-
#whiny ⇒ Object
Handles watermarking of images that are uploaded.
Instance Method Summary collapse
-
#convert_options? ⇒ Boolean
Returns true if the image is meant to make use of additional convert options.
-
#crop? ⇒ Boolean
Returns true if the
target_geometry
is meant to crop. - #fromfile ⇒ Object
-
#initialize(file, options = {}, attachment = nil) ⇒ Watermark
constructor
A new instance of Watermark.
-
#make ⇒ Object
Performs the conversion of the
file
into a watermark. - #tofile(destination) ⇒ Object
- #transformation_command ⇒ Object
Constructor Details
#initialize(file, options = {}, attachment = nil) ⇒ Watermark
Returns a new instance of Watermark.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/paperclip-watermark/watermark.rb', line 17 def initialize file, = {}, = nil super geometry = [:geometry] @crop = geometry[-1,1] == '#' if geometry.present? @file = file @target_geometry = Geometry.parse geometry @current_geometry = Geometry.from_file @file @convert_options = [:convert_options] @whiny = [:whiny].nil? ? true : [:whiny] @format = [:format] @watermark_path = [:watermark_path] @position = [:watermark_position].nil? ? "Center" : [:watermark_position] @overlay = [:watermark_overlay].nil? ? true : false @tile = [:watermark_tile].nil? ? false : [:watermark_tile] @watermark_option = [:watermark_option].nil? ? nil : [:watermark_option] @dissolve_option = [:dissolve_option].nil? ? nil : [:dissolve_option] @watermark_geometry = [:watermark_geometry].nil? ? nil : [:watermark_geometry] @watermark_resize = [:watermark_resize].nil? ? nil : [:watermark_resize] @current_format = File.extname(@file.path) @basename = File.basename(@file.path, @current_format) end |
Instance Attribute Details
#convert_options ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def @convert_options end |
#current_geometry ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def current_geometry @current_geometry end |
#dissolve_option ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def dissolve_option @dissolve_option end |
#format ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def format @format end |
#overlay ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def @overlay end |
#position ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def position @position end |
#target_geometry ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def target_geometry @target_geometry end |
#tile ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def tile @tile end |
#watermark_geometry ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def watermark_geometry @watermark_geometry end |
#watermark_option ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def watermark_option @watermark_option end |
#watermark_path ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def watermark_path @watermark_path end |
#watermark_resize ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def watermark_resize @watermark_resize end |
#whiny ⇒ Object
Handles watermarking of images that are uploaded.
13 14 15 |
# File 'lib/paperclip-watermark/watermark.rb', line 13 def whiny @whiny end |
Instance Method Details
#convert_options? ⇒ Boolean
Returns true if the image is meant to make use of additional convert options.
51 52 53 |
# File 'lib/paperclip-watermark/watermark.rb', line 51 def not [*@convert_options].reject(&:blank?).empty? end |
#crop? ⇒ Boolean
Returns true if the target_geometry
is meant to crop.
46 47 48 |
# File 'lib/paperclip-watermark/watermark.rb', line 46 def crop? @crop end |
#fromfile ⇒ Object
99 100 101 |
# File 'lib/paperclip-watermark/watermark.rb', line 99 def fromfile File.(@file.path) end |
#make ⇒ Object
Performs the conversion of the file
into a watermark. Returns the Tempfile that contains the new image.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/paperclip-watermark/watermark.rb', line 57 def make filename = [@basename, @format ? ".#{@format}" : ""].join dst = TempfileFactory.new.generate(filename) if watermark_path # unless _transformation_command.blank? # command = "convert" # params = [fromfile] # params += transformation_command # params << tofile(dst) # begin # success = Paperclip.run(command, params.flatten.compact.join(" ")) # rescue Paperclip::Errors::CommandNotFoundError # raise Paperclip::Errors::CommandNotFoundError, "There was an error resizing and cropping #{@basename}" if @whiny # end # end command = "composite" params = [] params += %W[-geometry #{watermark_geometry}] if watermark_geometry params += %W[-dissolve #{dissolve_option}] if dissolve_option params += %W[-watermark #{watermark_option}] if watermark_option if tile params += %W[-tile] else params += %W[-gravity #{position}] end if watermark_resize params += %W[#{"\\( #{watermark_path} -resize #{watermark_resize} \\)"} #{fromfile} #{tofile(dst)}] else params += %W[#{watermark_path} #{fromfile} #{tofile(dst)}] end begin success = Paperclip.run(command, params.flatten.compact.join(" ")) rescue Paperclip::Errors::CommandNotFoundError raise Paperclip::Errors::CommandNotFoundError, "There was an error processing the watermark for #{@basename}" if @whiny end end dst end |
#tofile(destination) ⇒ Object
103 104 105 |
# File 'lib/paperclip-watermark/watermark.rb', line 103 def tofile(destination) [@format, File.(destination.path)].compact.join(':') end |
#transformation_command ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/paperclip-watermark/watermark.rb', line 107 def transformation_command if @target_geometry.present? scale, crop = @current_geometry.transformation_to(@target_geometry, crop?) trans = [] trans += %W[-resize #{scale}] trans += %W[-crop #{crop} +repage] if crop trans += [*] if else scale, crop = @current_geometry.transformation_to(@current_geometry, crop?) trans = [] trans += %W[-resize #{scale}] if scale trans += %W[-crop #{crop} +repage] if crop trans += [*] if end trans end |