Skip to content

Commit

Permalink
Beginning of "!" geometry handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Porteneuve committed Aug 4, 2009
1 parent be7ee2f commit 6bb4970
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/geometry.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# This Geometry class was yanked from RMagick. However, it lets ImageMagick handle the actual change_geometry.
# Use #new_dimensions_for to get new dimensons
# Use #new_dimensions_for to get new dimensions
# Used so I can use spiffy RMagick geometry strings with ImageScience
class Geometry
# ! and @ are removed until support for them is added
FLAGS = ['', '%', '<', '>']#, '!', '@']
# @ is removed until support for them is added
FLAGS = ['', '%', '<', '>', '!']#, '@']
RFLAGS = { '%' => :percent,
'!' => :aspect,
'<' => :>,
Expand Down Expand Up @@ -59,6 +59,9 @@ def new_dimensions_for(orig_width, orig_height)
scale_y = @height.zero? ? @width : @height
new_width = scale_x.to_f * (orig_width.to_f / 100.0)
new_height = scale_y.to_f * (orig_height.to_f / 100.0)
when :aspect
new_width = @width unless @width.nil?
new_height = @height unless @height.nil?
when :<, :>, nil
scale_factor =
if new_width.zero? || new_height.zero?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ def resize_image(img, size)
end
else
new_size = [img.width, img.height] / size.to_s
img.resize(new_size[0], new_size[1], &grab_dimensions)
if size.ends_with?('!')
aspect = new_size[0].to_f / new_size[1].to_f
ih, iw = img.height, img.width
w, h = (ih * aspect), (iw / aspect)
w = [iw, w].min.to_i
h = [ih, h].min.to_i
img.with_crop((iw-w)/2, (ih-h)/2, (iw+w)/2, (ih+h)/2) { |crop|
crop.resize(new_size[0], new_size[1], &grab_dimensions)
}
else
img.resize(new_size[0], new_size[1], &grab_dimensions)
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/geometry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def test_should_resize_with_greater_and_no_height
"100>" => [50, 64]
end

def test_should_resize_with_aspect
assert_geometry 50, 64,
"35x35!" => [35, 35],
"70x70!" => [70, 70]
end

protected
def assert_geometry(width, height, values)
values.each do |geo, result|
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

$:.unshift(File.dirname(__FILE__) + '/../lib')

ENV['RAILS_ENV'] = 'test'
Expand Down

0 comments on commit 6bb4970

Please sign in to comment.