Class: Vcode

Inherits:
Object
  • Object
show all
Defined in:
lib/vcode.rb

Instance Method Summary collapse

Constructor Details

#initialize(w = 90, h = 40, fontsize = 24) ⇒ Vcode

Returns a new instance of Vcode.



2
3
4
# File 'lib/vcode.rb', line 2

def initialize(w=90,h=40,fontsize=24)
  @w=w and @h=h and @size=fontsize
end

Instance Method Details

#codeObject



37
38
39
# File 'lib/vcode.rb', line 37

def code
  @code
end

#getvcodeObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vcode.rb', line 5

def getvcode
  #创建画布
  img = Magick::Image.new(@w, @h) {
    self.background_color = 'white'
    self.format="JPG"
  }
  text= Magick::Draw.new
  text.pointsize = @size
  text.kerning = -1
  text.fill('blue')
  #随机文字
  @code=""
  4.times{@code << (97 + rand(26)).chr}
  #设置文字
  text.text(rand(@w/2-5),@h/2-5+ rand(@h/2), @code)
  #随机直线
  for i in 1..rand(4)
    text.line(rand(@w), rand(@h), rand(@w), rand(@h)) #直线
  end
  text.fill('blue')
  #燥点
  for i in 1..280
    text.point(rand(@w), rand(@h))
  end
  text.draw(img)
  #模糊
  img = img.sketch(0, 10, 50)
  #扭曲
  img = img.wave(5.5, 50)
  #返回图片数据流
  img.to_blob
end