Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add a scale resize method #1

Merged
merged 2 commits into from

1 participant

@kitwalker12
Owner

No description provided.

added some commits
@kitwalker12 Add a scale resize method
This can be used to scale an image preserving the aspect ratio. 
Ex: Scale an image of size 630x630 to 830x630. The extra width will have the extra width padded with the background color.
fd0b8b4
@kitwalker12 Adding tests for scaled resize ec222bf
@kitwalker12 kitwalker12 merged commit 992bf2c into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 4, 2014
  1. Add a scale resize method

    authored
    This can be used to scale an image preserving the aspect ratio. 
    Ex: Scale an image of size 630x630 to 830x630. The extra width will have the extra width padded with the background color.
This page is out of date. Refresh to see the latest.
View
4 Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- imanip (0.1.4)
+ imanip (0.1.5)
safe_shell (~> 1.0)
GEM
@@ -10,7 +10,7 @@ GEM
mocha (0.9.8)
rake
rake (0.8.7)
- safe_shell (1.0.0)
+ safe_shell (1.0.2)
PLATFORMS
ruby
View
23 lib/imanip/imanip_magick.rb
@@ -90,6 +90,22 @@ def crop_resize(to_file, options = {})
end
alias :crop_resized :crop_resize
+ def scale_resize(to_file, options = {})
+ @options = options.dup
+ parse_size_options!
+ scale_resize_string = ""
+ preserve_width = @geometry[:width]
+ preserve_height = @geometry[:height]
+ scale_width = @scale[:width]
+ scale_height = @scale[:height]
+ gravity = @options.delete(:gravity) || 'center'
+ background = @options.delete(:background) || 'white'
+ scale_resize_string << " -background #{background} -scale #{to_geometry_string(:height => preserve_height, :width => preserve_width)} -extent #{to_geometry_string(:height => scale_height, :width => scale_width)}"
+ scale_resize_string << " -gravity #{gravity}"
+ scale_resize_string << " #{options_to_string(@options)}"
+ convert(to_file,scale_resize_string)
+ end
+
def identify(options = {})
response = execute("#{execute_path}identify #{options_to_string(options)} #{@image_path}")
matches = response.match(/(JPEG|PNG|GIF)\ (\d+)x(\d+)/)
@@ -117,6 +133,7 @@ def convert(to_file,options = {})
def parse_size_options!
@geometry = {}
+ @scale = {}
if @options[:geometry]
width, height = @options.delete(:geometry).split('x')
@geometry = {:width => width, :height => height}
@@ -125,6 +142,10 @@ def parse_size_options!
width, height = @options.delete(:dimensions)
@geometry = {:width => width, :height => height}
end
+ if @options[:scale]
+ width, height = @options.delete(:scale)
+ @scale = {:width => width, :height => height}
+ end
@geometry = {:width => @options.delete(:width), :height => @options.delete(:height) } if @options[:width] || @options[:height]
@geometry.collect {|d| d[1].to_i unless d.nil? }
end
@@ -161,4 +182,4 @@ def execute_path
end
end
end
-end
+end
View
14 test/test_imanip_magick.rb
@@ -94,6 +94,20 @@ def test_crop_resize_should_crop_and_resize_image_to_exact_dimensions
assert_equal dimensions, @new_image.dimensions
end
+ def test_scale_resize_should_scale_and_resize_image_to_scaled_dimensions
+ dimensions = [400,315]
+ scaled = [600,315]
+ assert @landscape_image.scale_resize(new_image_path, :dimensions => dimensions, :scale => scaled)
+ @new_image = new_imanip_image(new_image_path)
+ assert_equal scaled, @new_image.dimensions
+
+ dimensions = [411,519]
+ scaled = [411,600]
+ assert @portrait_image.scale_resize(new_image_path, :dimensions => dimensions, :scale => scaled)
+ @new_image = new_imanip_image(new_image_path)
+ assert_equal scaled, @new_image.dimensions
+ end
+
def test_crop_resize_should_crop_and_resize_image_to_exact_dimensions_with_square_dimensions
dimensions = [100,100]
assert @landscape_image.crop_resize(new_image_path, :dimensions => dimensions)
Something went wrong with that request. Please try again.