Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

support proportionate scaling

  • Loading branch information...
commit 2b8df95f79f452b444368e404498db990c02482a 1 parent 4e6c214
Doug McInnes authored

Showing 2 changed files with 42 additions and 4 deletions. Show diff stats Hide diff stats

  1. +20 4 lib/fastimage_resize.rb
  2. +22 0 test/test.rb
24 lib/fastimage_resize.rb
@@ -44,6 +44,8 @@ class FormatNotSupported < FastImageException # :nodoc:
44 44 #
45 45 # FastImage Resize can resize GIF, JPEG and PNG files.
46 46 #
  47 + # Giving a zero value for width or height causes the image to scale proportionately.
  48 + #
47 49 # === Example
48 50 #
49 51 # require 'fastimage_resize'
@@ -83,7 +85,7 @@ def self.resize(input, w, h, options={})
83 85 end
84 86
85 87 in_path = file_in.respond_to?(:path) ? file_in.path : file_in
86   -
  88 +
87 89 fast_image.resize_image(in_path, file_out.to_s, w.to_i, h.to_i, type_index, jpeg_quality.to_i)
88 90
89 91 if file_in.respond_to?(:close)
@@ -122,13 +124,10 @@ def resize_image(filename_in, filename_out, w, h, image_type, jpeg_quality); end
122 124 in = fopen(filename_in, "rb");
123 125 if (!in) return Qnil;
124 126
125   - im_out = gdImageCreateTrueColor(w, h); /* must be truecolor */
126 127 switch(image_type) {
127 128 case 0: im_in = gdImageCreateFromJpeg(in);
128 129 break;
129 130 case 1: im_in = gdImageCreateFromPng(in);
130   - gdImageAlphaBlending(im_out, 0); /* handle transparency correctly */
131   - gdImageSaveAlpha(im_out, 1);
132 131 break;
133 132 case 2: im_in = gdImageCreateFromGif(in);
134 133 trans = gdImageGetTransparent(im_in);
@@ -150,6 +149,23 @@ def resize_image(filename_in, filename_out, w, h, image_type, jpeg_quality); end
150 149 break;
151 150 default: return Qnil;
152 151 }
  152 +
  153 + if (w == 0 || h == 0) {
  154 + int originalWidth = gdImageSX(im_in);
  155 + int originalHeight = gdImageSY(im_in);
  156 + if (w == 0) {
  157 + w = (int)(h * originalWidth / originalHeight);
  158 + } else {
  159 + h = (int)(w * originalHeight / originalWidth);
  160 + }
  161 + }
  162 +
  163 + im_out = gdImageCreateTrueColor(w, h); /* must be truecolor */
  164 +
  165 + if (image_type == 1) {
  166 + gdImageAlphaBlending(im_out, 0); /* handle transparency correctly */
  167 + gdImageSaveAlpha(im_out, 1);
  168 + }
153 169
154 170 fclose(in);
155 171
22 test/test.rb
@@ -136,4 +136,26 @@ def test_output_tempfile_has_right_extension
136 136 outfile = FastImage.resize(File.join(FixturePath, "test.png"), 200, 200)
137 137 assert outfile.path =~ /\.png$/
138 138 end
  139 +
  140 + def test_zero_width_scales_proportionately
  141 + GoodFixtures.each do |fn, info|
  142 + File.open(File.join(FixturePath, fn)) do |io|
  143 + halfHeight = (info[1][1] / 2).round
  144 + outfile = FastImage.resize(io, 0, halfHeight)
  145 + newWidth = (halfHeight * info[1][0] / info[1][1]).round
  146 + assert_equal [newWidth, halfHeight], FastImage.size(outfile)
  147 + end
  148 + end
  149 + end
  150 +
  151 + def test_zero_height_scales_proportionately
  152 + GoodFixtures.each do |fn, info|
  153 + File.open(File.join(FixturePath, fn)) do |io|
  154 + halfWidth = (info[1][0] / 2).round
  155 + outfile = FastImage.resize(io, halfWidth, 0)
  156 + newHeight = (halfWidth * info[1][1] / info[1][0]).round
  157 + assert_equal [halfWidth, newHeight], FastImage.size(outfile)
  158 + end
  159 + end
  160 + end
139 161 end

0 comments on commit 2b8df95

Please sign in to comment.
Something went wrong with that request. Please try again.