Skip to content
This repository
Browse code

Abstract out the file geometry parser within Thumbnail. This makes it…

… possible to have different cropping and scaling controls, as the papermill gem does.
  • Loading branch information...
commit 3f7aee300bcb1f2014904cf9df8224ece92ad2a9 1 parent d336c4a
Mike Burns authored August 24, 2011
20  gemfiles/rails2.gemfile
@@ -2,19 +2,19 @@
2 2
 
3 3
 source "http://rubygems.org"
4 4
 
  5
+gem "sqlite3", "~>1.3.4"
  6
+gem "rails", "~> 2.3.12"
5 7
 gem "activerecord", :require=>"active_record"
6  
-gem "appraisal"
7  
-gem "aws-s3", :require=>"aws/s3"
8  
-gem "bundler"
9 8
 gem "cocaine", "~>0.2"
10  
-gem "fog"
11  
-gem "jruby-openssl", :platform=>:jruby
  9
+gem "rake"
  10
+gem "sprockets", "~> 2.0.0.beta.13", :require=>false
  11
+gem "shoulda"
12 12
 gem "mime-types"
  13
+gem "jruby-openssl", :platform=>:jruby
13 14
 gem "mocha"
14  
-gem "rake"
  15
+gem "bundler"
  16
+gem "fog"
  17
+gem "appraisal"
  18
+gem "aws-s3", :require=>"aws/s3"
15 19
 gem "rdoc", :require=>false
16  
-gem "shoulda"
17  
-gem "sqlite3", "~>1.3.4"
18  
-gem "sprockets", "~> 2.0.0.beta.13", :require=>false
19  
-gem "rails", "~> 2.3.12"
20 20
 
20  gemfiles/rails3.gemfile
@@ -2,19 +2,19 @@
2 2
 
3 3
 source "http://rubygems.org"
4 4
 
  5
+gem "sqlite3", "~>1.3.4"
  6
+gem "rails", "~> 3.0.9"
5 7
 gem "activerecord", :require=>"active_record"
6  
-gem "appraisal"
7  
-gem "aws-s3", :require=>"aws/s3"
8  
-gem "bundler"
9 8
 gem "cocaine", "~>0.2"
10  
-gem "fog"
11  
-gem "jruby-openssl", :platform=>:jruby
  9
+gem "rake"
  10
+gem "sprockets", "~> 2.0.0.beta.13", :require=>false
  11
+gem "shoulda"
12 12
 gem "mime-types"
  13
+gem "jruby-openssl", :platform=>:jruby
13 14
 gem "mocha"
14  
-gem "rake"
  15
+gem "bundler"
  16
+gem "fog"
  17
+gem "appraisal"
  18
+gem "aws-s3", :require=>"aws/s3"
15 19
 gem "rdoc", :require=>false
16  
-gem "shoulda"
17  
-gem "sqlite3", "~>1.3.4"
18  
-gem "sprockets", "~> 2.0.0.beta.13", :require=>false
19  
-gem "rails", "~> 3.0.9"
20 20
 
20  gemfiles/rails3_1.gemfile
@@ -2,19 +2,19 @@
2 2
 
3 3
 source "http://rubygems.org"
4 4
 
  5
+gem "sqlite3", "~>1.3.4"
  6
+gem "rails", "~> 3.1.0.rc5"
5 7
 gem "activerecord", :require=>"active_record"
6  
-gem "appraisal"
7  
-gem "aws-s3", :require=>"aws/s3"
8  
-gem "bundler"
9 8
 gem "cocaine", "~>0.2"
10  
-gem "fog"
11  
-gem "jruby-openssl", :platform=>:jruby
  9
+gem "rake"
  10
+gem "sprockets", "~> 2.0.0.beta.13", :require=>false
  11
+gem "shoulda"
12 12
 gem "mime-types"
  13
+gem "jruby-openssl", :platform=>:jruby
13 14
 gem "mocha"
14  
-gem "rake"
  15
+gem "bundler"
  16
+gem "fog"
  17
+gem "appraisal"
  18
+gem "aws-s3", :require=>"aws/s3"
15 19
 gem "rdoc", :require=>false
16  
-gem "shoulda"
17  
-gem "sqlite3", "~>1.3.4"
18  
-gem "sprockets", "~> 2.0.0.beta.13", :require=>false
19  
-gem "rails", "~> 3.1.0.rc5"
20 20
 
16  lib/paperclip/thumbnail.rb
@@ -14,14 +14,24 @@ class Thumbnail < Processor
14 14
     # unless specified. Thumbnail creation will raise no errors unless
15 15
     # +whiny+ is true (which it is, by default. If +convert_options+ is
16 16
     # set, the options will be appended to the convert command upon image conversion
17  
-    def initialize file, options = {}, attachment = nil
  17
+    #
  18
+    # Options include:
  19
+    #
  20
+    #   +geometry+ - the desired width and height of the thumbnail
  21
+    #   +file_geometry_parser+ - an object with a method named +from_file+ that takes an image file and produces its geometry. Defaults to Paperclip::Geometry
  22
+    #   +source_file_options+ - flags passed to the +convert+ command that influence how the source file is read
  23
+    #   +convert_options+ - flags passed to the +convert+ command that influence how the image is processed
  24
+    #   +whiny+ - whether to raise an error when processing fails. Defaults to true.
  25
+    #   +format+ - the desired filename extension
  26
+    #   +animated+ - whether to merge all the layers in the image. Defaults to true.
  27
+    def initialize(file, options = {}, attachment = nil)
18 28
       super
19 29
 
20 30
       geometry             = options[:geometry]
21 31
       @file                = file
22 32
       @crop                = geometry[-1,1] == '#'
23  
-      @target_geometry     = Geometry.parse geometry
24  
-      @current_geometry    = Geometry.from_file @file
  33
+      @target_geometry     = Geometry.parse(geometry)
  34
+      @current_geometry    = (options[:file_geometry_parser] || Geometry).from_file(@file)
25 35
       @source_file_options = options[:source_file_options]
26 36
       @convert_options     = options[:convert_options]
27 37
       @whiny               = options[:whiny].nil? ? true : options[:whiny]
26  test/thumbnail_test.rb
@@ -224,6 +224,32 @@ class ThumbnailTest < Test::Unit::TestCase
224 224
         assert !@thumb.transformation_command.include?("-resize")
225 225
       end
226 226
     end
  227
+
  228
+    context "passing a custom geometry parser" do
  229
+      should "produce the appropriate transformation_command" do
  230
+        GeoParser = Class.new do
  231
+          def self.from_file(file)
  232
+            new
  233
+          end
  234
+          def transformation_to(target, should_crop)
  235
+            ["SCALE", "CROP"]
  236
+          end
  237
+        end
  238
+
  239
+        thumb = Paperclip::Thumbnail.new(@file, :geometry => "50x50", :file_geometry_parser => GeoParser)
  240
+
  241
+        transformation_command = thumb.transformation_command
  242
+
  243
+        assert transformation_command.include?('-crop'),
  244
+          %{expected #{transformation_command.inspect} to include '-crop'}
  245
+        assert transformation_command.include?('"CROP"'),
  246
+          %{expected #{transformation_command.inspect} to include '"CROP"'}
  247
+        assert transformation_command.include?('-resize'),
  248
+          %{expected #{transformation_command.inspect} to include '-resize'}
  249
+        assert transformation_command.include?('"SCALE"'),
  250
+          %{expected #{transformation_command.inspect} to include '"SCALE"'}
  251
+      end
  252
+    end
227 253
   end
228 254
 
229 255
   context "A multipage PDF" do

0 notes on commit 3f7aee3

Please sign in to comment.
Something went wrong with that request. Please try again.