From 978f54359e13b9776a9f344e87d17b92a9bbd093 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Thu, 28 Aug 2008 19:54:35 +0200 Subject: [PATCH] Changed geometry options to conform with ImageMagick resize options. --- lib/paperclip/geometry.rb | 20 ++++++++++---------- test/geometry_test.rb | 40 +++++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/lib/paperclip/geometry.rb b/lib/paperclip/geometry.rb index 8c00dde..4f1043a 100644 --- a/lib/paperclip/geometry.rb +++ b/lib/paperclip/geometry.rb @@ -1,15 +1,13 @@ module Paperclip - + # Defines the geometry of an image. class Geometry attr_accessor :height, :width, :modifier # Gives a Geometry representing the given height and width def initialize width = nil, height = nil, modifier = nil - height = nil if height == "" - width = nil if width == "" - @height = (height || width).to_f - @width = (width || height).to_f + @height = height.to_f + @width = width.to_f @modifier = modifier end @@ -17,13 +15,13 @@ module Paperclip # File or path. def self.from_file file file = file.path if file.respond_to? "path" - parse(`#{Paperclip.path_for_command('identify')} "#{file}"`) || + parse(`#{Paperclip.path_for_command('identify')} -format "%wx%h" "#{file}"`) || raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command.")) end # Parses a "WxH" formatted string, where W is the width and H is the height. def self.parse string - if match = (string && string.match(/\b(\d*)x(\d*)\b([\>\<\#\@\%^!])?/)) + if match = (string && string.match(/\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/)) Geometry.new(*match[1,3]) end end @@ -60,7 +58,10 @@ module Paperclip # Returns the width and height in a format suitable to be passed to Geometry.parse def to_s - "%dx%d%s" % [width, height, modifier] + s = "" + s << width.to_i.to_s if width > 0 + s << "x#{height.to_i}#{modifier}" if height > 0 + s end # Same as to_s @@ -76,7 +77,6 @@ module Paperclip # overhanging image would be cropped. Useful for square thumbnail images. The cropping # is weighted at the center of the Geometry. def transformation_to dst, crop = false - if crop ratio = Geometry.new( dst.width / self.width, dst.height / self.height ) scale_geometry, scale = scaling(dst, ratio) @@ -84,7 +84,7 @@ module Paperclip else scale_geometry = dst.to_s end - + [ scale_geometry, crop_geometry ] end diff --git a/test/geometry_test.rb b/test/geometry_test.rb index c803e12..8e65e32 100644 --- a/test/geometry_test.rb +++ b/test/geometry_test.rb @@ -12,15 +12,15 @@ class GeometryTest < Test::Unit::TestCase assert_equal 768, @geo.height end - should "correctly create a square if the height dimension is missing" do + should "set height to 0 if height dimension is missing" do assert @geo = Paperclip::Geometry.new(1024) assert_equal 1024, @geo.width - assert_equal 1024, @geo.height + assert_equal 0, @geo.height end - should "correctly create a square if the width dimension is missing" do + should "set width to 0 if width dimension is missing" do assert @geo = Paperclip::Geometry.new(nil, 768) - assert_equal 768, @geo.width + assert_equal 0, @geo.width assert_equal 768, @geo.height end @@ -32,14 +32,20 @@ class GeometryTest < Test::Unit::TestCase should "be generated from a xH-formatted string" do assert @geo = Paperclip::Geometry.parse("x600") - assert_equal 600, @geo.width + assert_equal 0, @geo.width assert_equal 600, @geo.height end - + should "be generated from a Wx-formatted string" do assert @geo = Paperclip::Geometry.parse("800x") assert_equal 800, @geo.width - assert_equal 800, @geo.height + assert_equal 0, @geo.height + end + + should "be generated from a W-formatted string" do + assert @geo = Paperclip::Geometry.parse("800") + assert_equal 800, @geo.width + assert_equal 0, @geo.height end should "ensure the modifier is nil if only one dimension present" do @@ -65,6 +71,26 @@ class GeometryTest < Test::Unit::TestCase assert_equal "123x456>", @src.transformation_to(@dst).to_s end + should "generate correct ImageMagick formatting string for W-formatted string" do + assert @geo = Paperclip::Geometry.parse("800") + assert_equal "800", @geo.to_s + end + + should "generate correct ImageMagick formatting string for Wx-formatted string" do + assert @geo = Paperclip::Geometry.parse("800x") + assert_equal "800", @geo.to_s + end + + should "generate correct ImageMagick formatting string for xH-formatted string" do + assert @geo = Paperclip::Geometry.parse("x600") + assert_equal "x600", @geo.to_s + end + + should "generate correct ImageMagick formatting string for WxH-formatted string" do + assert @geo = Paperclip::Geometry.parse("800x600") + assert_equal "800x600", @geo.to_s + end + should "be generated from a file" do file = File.join(File.dirname(__FILE__), "fixtures", "5k.png") file = File.new(file) -- 1.5.2.4