Created
February 5, 2010 07:26
-
-
Save qichunren/295602 to your computer and use it in GitHub Desktop.
验证码程序
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in app/models/ | |
class NoisyImage | |
require 'RMagick' | |
include Magick | |
attr_reader :code, :code_image | |
def initialize(length = 4) | |
text_size = 20 | |
validbglinecolors = ['#FFF', '#FFF', '#FFF', '#FFF', '#FFF'] | |
validchars = (0..9).to_a | |
step = -20 | |
chars = [] | |
text_size_step = text_size * 0.7 | |
length.times {|x| | |
chars << validchars[rand(validchars.size).ceil - 1].to_s | |
} | |
bglinecolor = validbglinecolors[1] | |
image = Image.new(length * text_size_step + 0, 24, HatchFill.new('white', bglinecolor, 1)) | |
text = Draw.new | |
#使用字体文件和直接使用字体,或者不写font, font_family两个属性都是如图所示结果,linux下无字显示 | |
#text.font = "fonts/arial.ttf"# + #['times.ttf', 'arial.ttf', 'verdana.ttf', 'artro.ttf'].sort{rand}.pop | |
#text.font_family = ['times', 'sans', 'fixed', 'Verdana'].sort{rand}.pop | |
text.font_weight = BoldWeight | |
text.text(0,0, ' ') | |
chars.each {|char| | |
text.annotate(image,0 , 0, step, 3, char) { | |
self.rotation = 5#dist[rand(dist.size - 1).ceil] | |
self.fill = "#000"#colors[rand(colors.size - 1).ceil] | |
self.pointsize = 22 | |
} | |
step += text_size_step - rand(5) | |
} | |
text.draw(image) | |
@code = chars.to_s() | |
@code_image = image.to_blob{ self.format="JPG" } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment