ruby的生成随机验证码

网上找的使用ruby生成的随机验证码图片

require 'rubygems'
require 'RMagick'

class ValidationImage
  include Magick
  attr_reader :code, :image
  Jiggle = 15
  Wobble = 15

  def initialize(len = 4)
    chars = ('a'..'z').to_a + ('0'..'9').to_a
    rand_chars = []
    1.upto(len) {rand_chars.push(chars[rand(chars.length)])}#生成验证码

    background_type = "netscape:" #彩条 "granite:" #花岗岩 "xc:#EDF7E7" #指定背景色 "null:" #纯黑
    background = Magick::ImageList.new(background_type)  #背景画布

    canvas = Magick::ImageList.new  #新建一个画布
    canvas.new_image(32*len, 30, Magick::TextureFill.new(background)) #把背景加到画布上

    gc = Magick::Draw.new  #新建一个画笔
    gc.font_family = 'times'
    gc.pointsize = 20
    cur = 10

    #在画布上把验证码写上去?
    rand_chars.each{|c|
      gc.annotate(canvas, 0, 0, cur, 15+rand(Jiggle), c){
        self.rotation = rand(10) > 5 ? rand(Wobble) : -rand(Wobble)
        self.font_weight = rand(10) > 5 ?  NormalWeight : BoldWeight
        self.fill = 'green'
      }
      cur += 30
    }

    @code = rand_chars.to_s
    @image = canvas.to_blob{
      self.format="GIF"
    }
  end
end


v=ValidationImage.new
#生成图片
filename= File.new('c:\1.jpg',"w+")
filename.binmode

puts v.code

filename.puts v.image
filename.puts v.code

你可能感兴趣的:(C++,c,C#,Ruby,rubygems)