Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* added size optoion.

  • Loading branch information...
commit a58feeae816714f4a2a8211c2fca903e796f2251 1 parent 0ba3c51
@swdyh authored
Showing with 39 additions and 2 deletions.
  1. +24 −2 lib/quilt.rb
  2. +15 −0 test/quilt_test.rb
View
26 lib/quilt.rb
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
require 'rubygems'
require 'digest/sha1'
@@ -34,13 +35,16 @@ def polygon points, color
end
def write path
- # @image.write path
open(path, 'w') {|f| f.puts @image.to_blob }
end
def to_blob
@image.to_blob
end
+
+ def resize size
+ @image.resize! size, size
+ end
end
class ImageGD
@@ -78,6 +82,13 @@ def write path
def to_blob
@image.pngStr
end
+
+ def resize size
+ _image = GD::Image.new size, size
+ # FIXME bug
+ @image.copyResized _image, 0, 0, 0, 0, size, size, @image.width, @image.height
+ @image = _image
+ end
end
class Identicon
@@ -115,13 +126,24 @@ def initialize str = '', opt = {}
@code = Identicon.calc_code str.to_s
end
@decode = decode @code
- @scale = opt[:scale] || 1
+
+ if opt[:size]
+ @scale = (((opt[:size] / 3) - 1) / (PATCH_SIZE - 1)) + 1
+ @resize_to = opt[:size]
+ else
+ @scale = opt[:scale] || 1
+ end
+
@patch_width = (PATCH_SIZE - 1) * @scale + 1
@image = @@image_lib.new @patch_width * 3, @patch_width * 3
@back_color = @image.color 255, 255, 255
@fore_color = @image.color @decode[:red], @decode[:green], @decode[:blue]
@image.transparent @back_color
render
+
+ if @resize_to
+ @image.resize @resize_to
+ end
end
def decode code
View
15 test/quilt_test.rb
@@ -70,4 +70,19 @@ def test_salt
assert_equal salt, Quilt::Identicon.salt
end
end
+
+ def test_size_opt_im
+ size = 100
+ Quilt::Identicon.image_lib = Quilt::ImageRmagick
+ identicon = Quilt::Identicon.new 'foo', :size => size
+ assert_equal size, identicon.instance_variable_get(:@image).instance_variable_get(:@image).rows
+ assert_equal size, identicon.instance_variable_get(:@image).instance_variable_get(:@image).columns
+ end
+
+ def test_size_opt_gd
+ Quilt::Identicon.image_lib = Quilt::ImageGD
+ identicon = Quilt::Identicon.new 'foo', :size => size
+ assert_equal size, identicon.instance_variable_get(:@image).instance_variable_get(:@image).width
+ assert_equal size, identicon.instance_variable_get(:@image).instance_variable_get(:@image).height
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.