public
Rubygem
Fork of swdyh/quilt
Description: a ruby library for generating identicon
Clone URL: git://github.com/crossblaim/quilt.git
 * added size optoion.
swdyh (author)
Sun Jun 08 04:53:27 -0700 2008
commit  a58feeae816714f4a2a8211c2fca903e796f2251
tree    2e2112f0d2bc0e421c725bc64dfd0a73980cd1c6
parent  0ba3c51a43ad8051510bfa7a74fe2bb5b87e315f
...
 
1
2
3
...
34
35
36
37
38
39
40
41
42
43
 
 
 
 
44
45
46
...
78
79
80
 
 
 
 
 
 
 
81
82
83
...
115
116
117
118
 
 
 
 
 
 
 
 
119
120
121
122
123
124
 
 
 
 
125
126
127
...
1
2
3
4
...
35
36
37
 
38
39
40
41
42
43
44
45
46
47
48
49
50
...
82
83
84
85
86
87
88
89
90
91
92
93
94
...
126
127
128
 
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
0
@@ -1,3 +1,4 @@
0
+# -*- coding: utf-8 -*-
0
 require 'rubygems'
0
 require 'digest/sha1'
0
 
0
@@ -34,13 +35,16 @@ module Quilt
0
     end
0
 
0
     def write path
0
- # @image.write path
0
       open(path, 'w') {|f| f.puts @image.to_blob }
0
     end
0
 
0
     def to_blob
0
       @image.to_blob
0
     end
0
+
0
+ def resize size
0
+ @image.resize! size, size
0
+ end
0
   end
0
 
0
   class ImageGD
0
@@ -78,6 +82,13 @@ module Quilt
0
     def to_blob
0
       @image.pngStr
0
     end
0
+
0
+ def resize size
0
+ _image = GD::Image.new size, size
0
+ # FIXME bug
0
+ @image.copyResized _image, 0, 0, 0, 0, size, size, @image.width, @image.height
0
+ @image = _image
0
+ end
0
   end
0
 
0
   class Identicon
0
@@ -115,13 +126,24 @@ module Quilt
0
         @code = Identicon.calc_code str.to_s
0
       end
0
       @decode = decode @code
0
- @scale = opt[:scale] || 1
0
+
0
+ if opt[:size]
0
+ @scale = (((opt[:size] / 3) - 1) / (PATCH_SIZE - 1)) + 1
0
+ @resize_to = opt[:size]
0
+ else
0
+ @scale = opt[:scale] || 1
0
+ end
0
+
0
       @patch_width = (PATCH_SIZE - 1) * @scale + 1
0
       @image = @@image_lib.new @patch_width * 3, @patch_width * 3
0
       @back_color = @image.color 255, 255, 255
0
       @fore_color = @image.color @decode[:red], @decode[:green], @decode[:blue]
0
       @image.transparent @back_color
0
       render
0
+
0
+ if @resize_to
0
+ @image.resize @resize_to
0
+ end
0
     end
0
 
0
     def decode code
...
70
71
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
0
@@ -70,4 +70,19 @@ class QultTest < Test::Unit::TestCase
0
       assert_equal salt, Quilt::Identicon.salt
0
     end
0
   end
0
+
0
+ def test_size_opt_im
0
+ size = 100
0
+ Quilt::Identicon.image_lib = Quilt::ImageRmagick
0
+ identicon = Quilt::Identicon.new 'foo', :size => size
0
+ assert_equal size, identicon.instance_variable_get(:@image).instance_variable_get(:@image).rows
0
+ assert_equal size, identicon.instance_variable_get(:@image).instance_variable_get(:@image).columns
0
+ end
0
+
0
+ def test_size_opt_gd
0
+ Quilt::Identicon.image_lib = Quilt::ImageGD
0
+ identicon = Quilt::Identicon.new 'foo', :size => size
0
+ assert_equal size, identicon.instance_variable_get(:@image).instance_variable_get(:@image).width
0
+ assert_equal size, identicon.instance_variable_get(:@image).instance_variable_get(:@image).height
0
+ end
0
 end

Comments

    No one has commented yet.