Class: EasyCaptcha::Generator::Default

Inherits:
Base
  • Object
show all
Defined in:
lib/easy_captcha/generator/default.rb

Overview

default generator

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from EasyCaptcha::Generator::Base

Instance Attribute Details

#blurObject

Gaussian Blur



44
45
46
# File 'lib/easy_captcha/generator/default.rb', line 44

def blur
  @blur
end

#blur_radiusObject

Gaussian Blur



44
45
46
# File 'lib/easy_captcha/generator/default.rb', line 44

def blur_radius
  @blur_radius
end

#blur_sigmaObject

Gaussian Blur



44
45
46
# File 'lib/easy_captcha/generator/default.rb', line 44

def blur_sigma
  @blur_sigma
end

#font_familyObject

Font



29
30
31
# File 'lib/easy_captcha/generator/default.rb', line 29

def font_family
  @font_family
end

#font_fill_colorObject

Font



29
30
31
# File 'lib/easy_captcha/generator/default.rb', line 29

def font_fill_color
  @font_fill_color
end

#font_sizeObject

Font



29
30
31
# File 'lib/easy_captcha/generator/default.rb', line 29

def font_size
  @font_size
end

#font_strokeObject

Font



29
30
31
# File 'lib/easy_captcha/generator/default.rb', line 29

def font_stroke
  @font_stroke
end

#font_stroke_colorObject

Font



29
30
31
# File 'lib/easy_captcha/generator/default.rb', line 29

def font_stroke_color
  @font_stroke_color
end

#image_background_colorObject

Background



32
33
34
# File 'lib/easy_captcha/generator/default.rb', line 32

def image_background_color
  @image_background_color
end

#implodeObject

Implode



41
42
43
# File 'lib/easy_captcha/generator/default.rb', line 41

def implode
  @implode
end

#sketchObject

Sketch



35
36
37
# File 'lib/easy_captcha/generator/default.rb', line 35

def sketch
  @sketch
end

#sketch_radiusObject

Sketch



35
36
37
# File 'lib/easy_captcha/generator/default.rb', line 35

def sketch_radius
  @sketch_radius
end

#sketch_sigmaObject

Sketch



35
36
37
# File 'lib/easy_captcha/generator/default.rb', line 35

def sketch_sigma
  @sketch_sigma
end

#waveObject

Wave



38
39
40
# File 'lib/easy_captcha/generator/default.rb', line 38

def wave
  @wave
end

#wave_amplitudeObject

Wave



38
39
40
# File 'lib/easy_captcha/generator/default.rb', line 38

def wave_amplitude
  @wave_amplitude
end

#wave_lengthObject

Wave



38
39
40
# File 'lib/easy_captcha/generator/default.rb', line 38

def wave_length
  @wave_length
end

Instance Method Details

#blur?Boolean

:nodoc:

Returns:

  • (Boolean)


54
55
56
# File 'lib/easy_captcha/generator/default.rb', line 54

def blur? #:nodoc:
  @blur
end

#defaultsObject

set default values



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/easy_captcha/generator/default.rb', line 9

def defaults
  @font_size              = 24
  @font_fill_color        = '#333333'
  @font_family            = File.expand_path('../../../../resources/captcha.ttf', __FILE__)
  @font_stroke            = '#000000'
  @font_stroke_color      = 0
  @image_background_color = '#FFFFFF'
  @sketch                 = true
  @sketch_radius          = 3
  @sketch_sigma           = 1
  @wave                   = true
  @wave_length            = (60..100)
  @wave_amplitude         = (3..5)
  @implode                = 0.05
  @blur                   = true
  @blur_radius            = 1
  @blur_sigma             = 2
end

#generate(code) ⇒ Object

generate image



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/easy_captcha/generator/default.rb', line 59

def generate(code)
  config = self
  canvas = Magick::Image.new(EasyCaptcha.image_width, EasyCaptcha.image_height) do |variable|
    self.background_color = config.image_background_color unless config.image_background_color.nil?
  end

  # Render the text in the image
  canvas.annotate(Magick::Draw.new, 0, 0, 0, 0, code) {
    self.gravity     = Magick::CenterGravity
    self.font_family = config.font_family
    self.font_weight = Magick::LighterWeight
    self.fill        = config.font_fill_color
    if config.font_stroke.to_i > 0
      self.stroke       = config.font_stroke_color
      self.stroke_width = config.font_stroke
    end
    self.pointsize = config.font_size
  }

  # Blur
  canvas = canvas.blur_image(config.blur_radius, config.blur_sigma) if config.blur?

  # Wave
  w = config.wave_length
  a = config.wave_amplitude
  canvas = canvas.wave(rand(a.last - a.first) + a.first, rand(w.last - w.first) + w.first) if config.wave?

  # Sketch
  canvas = canvas.sketch(config.sketch_radius, config.sketch_sigma, rand(180)) if config.sketch?

  # Implode
  canvas = canvas.implode(config.implode.to_f) if config.implode.is_a? Float

  # Crop image because to big after waveing
  canvas = canvas.crop(Magick::CenterGravity, EasyCaptcha.image_width, EasyCaptcha.image_height)

  image = canvas.to_blob { self.format = 'PNG' }

  # ruby-1.9
  image = image.force_encoding 'UTF-8' if image.respond_to? :force_encoding

  canvas.destroy!
  image
end

#sketch?Boolean

:nodoc:

Returns:

  • (Boolean)


46
47
48
# File 'lib/easy_captcha/generator/default.rb', line 46

def sketch? #:nodoc:
  @sketch
end

#wave?Boolean

:nodoc:

Returns:

  • (Boolean)


50
51
52
# File 'lib/easy_captcha/generator/default.rb', line 50

def wave? #:nodoc:
  @wave
end