diff --git a/lib/smartcropper.rb b/lib/smartcropper.rb index a69ab36..f0eb943 100644 --- a/lib/smartcropper.rb +++ b/lib/smartcropper.rb @@ -110,6 +110,7 @@ def smart_crop_by_trim(requested_x, requested_y) bottom -= slice_height end + break if slice_height == step_size height = (bottom - top) end end diff --git a/test/fixtures/small1.png b/test/fixtures/small1.png new file mode 100644 index 0000000..111c5b1 Binary files /dev/null and b/test/fixtures/small1.png differ diff --git a/test/fixtures/small2.png b/test/fixtures/small2.png new file mode 100644 index 0000000..e37e08d Binary files /dev/null and b/test/fixtures/small2.png differ diff --git a/test/helper.rb b/test/helper.rb index af575fc..1548dfd 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -13,5 +13,12 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) + +class Test::Unit::TestCase + def fixture_path + Pathname.new(File.join(File.expand_path(File.dirname(__FILE__)), "fixtures")) + end +end + require 'smartcropper' diff --git a/test/test_croptoelie.rb b/test/test_croptoelie.rb index 5217b6b..ac10610 100644 --- a/test/test_croptoelie.rb +++ b/test/test_croptoelie.rb @@ -14,6 +14,7 @@ def setup img = SmartCropper.new(@image) assert_equal(img.class, SmartCropper) end + should "create a smartcropper from an imagefile" do img = SmartCropper.from_file(@filename) assert_equal(img.class, SmartCropper) @@ -64,6 +65,24 @@ def setup assert_equal(img.image, @image) end + ########################################################################### + # Stuck in a loop when image is too small to slice # + ########################################################################### + should "handle 1px wide images" do + @filename = fixture_path.join("small1.png") + @image = Magick::ImageList.new(@filename).last + img = SmartCropper.new(@image).smart_crop_and_scale(1, 50) + size = [img.rows, img.columns] + assert_equal([50, 1], size) + end + + should "handle 5px wide images" do + @filename = fixture_path.join("small2.png") + @image = Magick::ImageList.new(@filename).last + img = SmartCropper.new(@image).smart_crop_and_scale(1, 50) + size = [img.rows, img.columns] + assert_equal([50, 1], size) + end ########################################################################### # Images reported to fail by issue #5 #