Permalink
Browse files

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...
1 parent d336c4a commit 3f7aee300bcb1f2014904cf9df8224ece92ad2a9 @mike-burns mike-burns committed Aug 24, 2011
Showing with 69 additions and 33 deletions.
  1. +10 −10 gemfiles/rails2.gemfile
  2. +10 −10 gemfiles/rails3.gemfile
  3. +10 −10 gemfiles/rails3_1.gemfile
  4. +13 −3 lib/paperclip/thumbnail.rb
  5. +26 −0 test/thumbnail_test.rb
View
@@ -2,19 +2,19 @@
source "http://rubygems.org"
+gem "sqlite3", "~>1.3.4"
+gem "rails", "~> 2.3.12"
gem "activerecord", :require=>"active_record"
-gem "appraisal"
-gem "aws-s3", :require=>"aws/s3"
-gem "bundler"
gem "cocaine", "~>0.2"
-gem "fog"
-gem "jruby-openssl", :platform=>:jruby
+gem "rake"
+gem "sprockets", "~> 2.0.0.beta.13", :require=>false
+gem "shoulda"
gem "mime-types"
+gem "jruby-openssl", :platform=>:jruby
gem "mocha"
-gem "rake"
+gem "bundler"
+gem "fog"
+gem "appraisal"
+gem "aws-s3", :require=>"aws/s3"
gem "rdoc", :require=>false
-gem "shoulda"
-gem "sqlite3", "~>1.3.4"
-gem "sprockets", "~> 2.0.0.beta.13", :require=>false
-gem "rails", "~> 2.3.12"
View
@@ -2,19 +2,19 @@
source "http://rubygems.org"
+gem "sqlite3", "~>1.3.4"
+gem "rails", "~> 3.0.9"
gem "activerecord", :require=>"active_record"
-gem "appraisal"
-gem "aws-s3", :require=>"aws/s3"
-gem "bundler"
gem "cocaine", "~>0.2"
-gem "fog"
-gem "jruby-openssl", :platform=>:jruby
+gem "rake"
+gem "sprockets", "~> 2.0.0.beta.13", :require=>false
+gem "shoulda"
gem "mime-types"
+gem "jruby-openssl", :platform=>:jruby
gem "mocha"
-gem "rake"
+gem "bundler"
+gem "fog"
+gem "appraisal"
+gem "aws-s3", :require=>"aws/s3"
gem "rdoc", :require=>false
-gem "shoulda"
-gem "sqlite3", "~>1.3.4"
-gem "sprockets", "~> 2.0.0.beta.13", :require=>false
-gem "rails", "~> 3.0.9"
View
@@ -2,19 +2,19 @@
source "http://rubygems.org"
+gem "sqlite3", "~>1.3.4"
+gem "rails", "~> 3.1.0.rc5"
gem "activerecord", :require=>"active_record"
-gem "appraisal"
-gem "aws-s3", :require=>"aws/s3"
-gem "bundler"
gem "cocaine", "~>0.2"
-gem "fog"
-gem "jruby-openssl", :platform=>:jruby
+gem "rake"
+gem "sprockets", "~> 2.0.0.beta.13", :require=>false
+gem "shoulda"
gem "mime-types"
+gem "jruby-openssl", :platform=>:jruby
gem "mocha"
-gem "rake"
+gem "bundler"
+gem "fog"
+gem "appraisal"
+gem "aws-s3", :require=>"aws/s3"
gem "rdoc", :require=>false
-gem "shoulda"
-gem "sqlite3", "~>1.3.4"
-gem "sprockets", "~> 2.0.0.beta.13", :require=>false
-gem "rails", "~> 3.1.0.rc5"
View
@@ -14,14 +14,24 @@ class Thumbnail < Processor
# unless specified. Thumbnail creation will raise no errors unless
# +whiny+ is true (which it is, by default. If +convert_options+ is
# set, the options will be appended to the convert command upon image conversion
- def initialize file, options = {}, attachment = nil
+ #
+ # 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
+ # +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.
+ # +format+ - the desired filename extension
+ # +animated+ - whether to merge all the layers in the image. Defaults to true.
+ def initialize(file, options = {}, attachment = nil)
super
geometry = options[:geometry]
@file = file
@crop = geometry[-1,1] == '#'
- @target_geometry = Geometry.parse geometry
- @current_geometry = Geometry.from_file @file
+ @target_geometry = 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]
@whiny = options[:whiny].nil? ? true : options[:whiny]
View
@@ -224,6 +224,32 @@ class ThumbnailTest < Test::Unit::TestCase
assert !@thumb.transformation_command.include?("-resize")
end
end
+
+ context "passing a custom geometry parser" do
+ should "produce the appropriate transformation_command" do
+ GeoParser = Class.new do
+ def self.from_file(file)
+ new
+ end
+ def transformation_to(target, should_crop)
+ ["SCALE", "CROP"]
+ end
+ end
+
+ thumb = Paperclip::Thumbnail.new(@file, :geometry => "50x50", :file_geometry_parser => GeoParser)
+
+ transformation_command = thumb.transformation_command
+
+ assert transformation_command.include?('-crop'),
+ %{expected #{transformation_command.inspect} to include '-crop'}
+ assert transformation_command.include?('"CROP"'),
+ %{expected #{transformation_command.inspect} to include '"CROP"'}
+ assert transformation_command.include?('-resize'),
+ %{expected #{transformation_command.inspect} to include '-resize'}
+ assert transformation_command.include?('"SCALE"'),
+ %{expected #{transformation_command.inspect} to include '"SCALE"'}
+ end
+ end
end
context "A multipage PDF" do

0 comments on commit 3f7aee3

Please sign in to comment.