Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Abstract out the file geometry parser within Thumbnail. This makes it…
Browse files Browse the repository at this point in the history
… possible to have different cropping and scaling controls, as the papermill gem does.
  • Loading branch information
mike-burns committed Aug 24, 2011
1 parent d336c4a commit 3f7aee3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 33 deletions.
20 changes: 10 additions & 10 deletions gemfiles/rails2.gemfile
Expand Up @@ -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"

20 changes: 10 additions & 10 deletions gemfiles/rails3.gemfile
Expand Up @@ -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"

20 changes: 10 additions & 10 deletions gemfiles/rails3_1.gemfile
Expand Up @@ -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"

16 changes: 13 additions & 3 deletions lib/paperclip/thumbnail.rb
Expand Up @@ -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]
Expand Down
26 changes: 26 additions & 0 deletions test/thumbnail_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit 3f7aee3

Please sign in to comment.