lib/papermill/papermill_paperclip_processor.rb in papermill-1.1.6 vs lib/papermill/papermill_paperclip_processor.rb in papermill-1.2.0
- old
+ new
@@ -1,37 +1,45 @@
module Paperclip
# Handles thumbnailing images that are uploaded.
class PapermillPaperclipProcessor < Thumbnail
- attr_accessor :crop_h, :crop_w, :crop_x, :crop_y, :copyright
+ attr_accessor :crop_h, :crop_w, :crop_x, :crop_y, :copyright, :watermark_path
def initialize(file, options = {}, attachment = nil)
@crop_h, @crop_w, @crop_x, @crop_y = options[:crop_h], options[:crop_w], options[:crop_x], options[:crop_y]
- if options[:geometry] =~ /©/
- options[:geometry], *@copyright = options[:geometry].split("©")
- @copyright = @copyright.join("©").nie || options[:copyright] || @attachment.instance.respond_to?(:copyright) && @attachment.instance.copyright
+ # copyright extraction
+ if options[:geometry] =~ /©/ || options[:copyright]
+ options[:geometry], *@copyright = options[:geometry].split("©", -1)
+ @copyright = options[:copyright] || @copyright.join("©").nie || file.instance.respond_to?(:copyright) && file.instance.copyright.nie || file.instance.papermill_options[:copyright].nie
+ @copyright = (options[:copyright_text_transform] || file.instance.papermill_options[:copyright_text_transform]).try(:call, @copyright) || @copyright if @copyright
end
+ # watermark extraction
+ if options[:watermark] || options[:geometry] =~ /\-wm/
+ options[:geometry] = options[:geometry].chomp("-wm")
+ @watermark_path = options[:watermark].is_a?(String) && options[:watermark] || file.instance.papermill_options[:watermark]
+ @watermark_path = file.instance.papermill_options[:public_root].sub(":rails_root", RAILS_ROOT) + @watermark_path if @watermark_path.starts_with?("/")
+ end
+
+
if options[:geometry] =~ /#.+/
+ # let's parse :
+ # <width>x<height>#<crop_w>x<crop_h>:<crop_x>:<crop_y>
+ # <width>x<height>#<crop_w>x<crop_h>
+ # <width>x<height>#<crop_x>:<crop_y>
- # <@target_geometry.width>x<@target_geometry.height>#<@crop_w>x<@crop_h>:<@crop_x>:<@crop_y>
- # <@target_geometry.width>x<@target_geometry.height>#<@crop_w>x<@crop_h>
- # <@target_geometry.width>x<@target_geometry.height>#<@crop_x>:<@crop_y>
-
options[:geometry], manual_crop = options[:geometry].split("#")
-
crop_dimensions, @crop_x, @crop_y = manual_crop.split("+")
-
+
if crop_dimensions =~ /x/
@crop_w, @crop_h = crop_dimensions.split("x")
else
@crop_x, @crop_y = crop_dimensions, @crop_x
end
-
-
+
options[:geometry] = (options[:geometry].nie || "#{@crop_x}x#{@crop_y}") + "#"
unless @crop_w && @crop_h
@target_geometry = Geometry.parse(options[:geometry])
@crop_w ||= @target_geometry.try(:width).to_i
@@ -40,12 +48,18 @@
end
super
end
def transformation_command
- #puts "crop_command= #{crop_command ? super.sub(/ -crop \S+/, crop_command) : super}"
-
- crop_command ? super.sub(/ -crop \S+/, crop_command) : super
+ "#{(crop_command ? super.sub(/ -crop \S+/, crop_command) : super)} #{copyright_command} #{watermark_command}".sub(%{-resize "0x" }, "")
+ end
+
+ def copyright_command
+ (options[:copyright_im_command] || @file.instance.papermill_options[:copyright_im_command]).gsub(/%s/, @copyright.gsub(/'/, %{'"'"'}).sub("-slash-", "/").sub("-backslash-", %{\\\\\\})) if @copyright
+ end
+
+ def watermark_command
+ (options[:watermark_im_command] || @file.instance.papermill_options[:watermark_im_command]).gsub(/%s/, @watermark_path) if @watermark_path
end
def crop_command
if @crop_h || @crop_x
" -crop '%dx%d+%d+%d'" % [ @crop_w, @crop_h, @crop_x, @crop_y ].map(&:to_i)