Class: Captcha::Image

Inherits:
Object
  • Object
show all
Includes:
Magick
Defined in:
lib/captcha/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(o) ⇒ Image

Returns a new instance of Image.



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
# File 'lib/captcha/image.rb', line 9

def initialize(o)
  generate_code o

  canvas = Magick::Image.new(o[:dimensions][:width], o[:dimensions][:height]) {
    self.background_color = o[:colors][:background]
  }
  
  text = Magick::Draw.new
  text.font = File.expand_path o[:ttf]
  text.pointsize = o[:letters][:points]
  
  cur = 0
  @code.each { |c|
    text.annotate(canvas, 0, 0, cur, o[:letters][:baseline], c) {
      self.fill = o[:colors][:font]
    }
    cur += o[:letters][:width]
  }
  
  w = o[:wave][:wavelength]
  canvas = canvas.wave(o[:wave][:amplitude], rand(w.last - w.first) + w.first)
  canvas = canvas.implode(o[:implode])
  
  @code = @code.to_s
  @data = canvas.to_blob { self.format = "JPG" }
  canvas.destroy!
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/captcha/image.rb', line 7

def code
  @code
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/captcha/image.rb', line 7

def data
  @data
end