diff --git a/lib/paperclip/thumbnail.rb b/lib/paperclip/thumbnail.rb index 848761e4d..49b8350ec 100644 --- a/lib/paperclip/thumbnail.rb +++ b/lib/paperclip/thumbnail.rb @@ -18,19 +18,20 @@ class Thumbnail < Processor # Options include: # # +geometry+ - the desired width and height of the thumbnail - # +file_geometry_parser+ - an object with a method named +from_file+ that takes an image file and produces its geometry. Defaults to Paperclip::Geometry + # +file_geometry_parser+ - an object with a method named +from_file+ that takes an image file and produces its geometry and a +transformation_to+. Defaults to Paperclip::Geometry + # +string_geometry_parser+ - an object with a method named +parse+ that takes a string and produces an object with +width+, +height+, and +to_s+ accessors. Defaults to Paperclip::Geometry # +source_file_options+ - flags passed to the +convert+ command that influence how the source file is read # +convert_options+ - flags passed to the +convert+ command that influence how the image is processed - # +whiny+ - whether to raise an error when processing fails. Defaults to true. + # +whiny+ - whether to raise an error when processing fails. Defaults to true # +format+ - the desired filename extension - # +animated+ - whether to merge all the layers in the image. Defaults to true. + # +animated+ - whether to merge all the layers in the image. Defaults to true def initialize(file, options = {}, attachment = nil) super - geometry = options[:geometry] + geometry = options[:geometry] # this is not an option @file = file @crop = geometry[-1,1] == '#' - @target_geometry = Geometry.parse(geometry) + @target_geometry = (options[:string_geometry_parser] || Geometry).parse(geometry) @current_geometry = (options[:file_geometry_parser] || Geometry).from_file(@file) @source_file_options = options[:source_file_options] @convert_options = options[:convert_options] diff --git a/test/thumbnail_test.rb b/test/thumbnail_test.rb index 6c8ffa79b..eb5c6c2ae 100644 --- a/test/thumbnail_test.rb +++ b/test/thumbnail_test.rb @@ -225,7 +225,7 @@ class ThumbnailTest < Test::Unit::TestCase end end - context "passing a custom geometry parser" do + context "passing a custom file geometry parser" do should "produce the appropriate transformation_command" do GeoParser = Class.new do def self.from_file(file) @@ -236,7 +236,7 @@ def transformation_to(target, should_crop) end end - thumb = Paperclip::Thumbnail.new(@file, :geometry => "50x50", :file_geometry_parser => GeoParser) + thumb = Paperclip::Thumbnail.new(@file, :geometry => '50x50', :file_geometry_parser => GeoParser) transformation_command = thumb.transformation_command @@ -250,6 +250,27 @@ def transformation_to(target, should_crop) %{expected #{transformation_command.inspect} to include '"SCALE"'} end end + + context "passing a custom geometry string parser" do + should "produce the appropriate transformation_command" do + GeoParser = Class.new do + def self.parse(s) + new + end + + def to_s + "151x167" + end + end + + thumb = Paperclip::Thumbnail.new(@file, :geometry => '50x50', :string_geometry_parser => GeoParser) + + transformation_command = thumb.transformation_command + + assert transformation_command.include?('"151x167"'), + %{expected #{transformation_command.inspect} to include '151x167'} + end + end end context "A multipage PDF" do