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

#background_imageObject

Background



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

def background_image
  @background_image
end

#blurObject

Gaussian Blur



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

def blur
  @blur
end

#blur_radiusObject

Gaussian Blur



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

def blur_radius
  @blur_radius
end

#blur_sigmaObject

Gaussian Blur



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

def blur_sigma
  @blur_sigma
end

#fontObject

Font



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

def font
  @font
end

#font_familyObject

Font



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

def font_family
  @font_family
end

#font_fill_colorObject

Font



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

def font_fill_color
  @font_fill_color
end

#font_sizeObject

Font



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

def font_size
  @font_size
end

#font_strokeObject

Font



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

def font_stroke
  @font_stroke
end

#font_stroke_colorObject

Font



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

def font_stroke_color
  @font_stroke_color
end

#image_background_colorObject

Background



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

def image_background_color
  @image_background_color
end

#implodeObject

Implode



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

def implode
  @implode
end

#sketchObject

Sketch



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

def sketch
  @sketch
end

#sketch_radiusObject

Sketch



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

def sketch_radius
  @sketch_radius
end

#sketch_sigmaObject

Sketch



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

def sketch_sigma
  @sketch_sigma
end

#waveObject

Wave



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

def wave
  @wave
end

#wave_amplitudeObject

Wave



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

def wave_amplitude
  @wave_amplitude
end

#wave_lengthObject

Wave



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

def wave_length
  @wave_length
end

Instance Method Details

#blur?Boolean

:nodoc:

Returns:

  • (Boolean)


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

def blur? #:nodoc:
  @blur
end

#defaultsObject

set default values



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

def defaults
  @font_size              = 24
  @font_fill_color        = '#333333'
  @font                   = 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



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
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/easy_captcha/generator/default.rb', line 60

def generate(code)
  require 'rmagick' unless defined?(Magick)

  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?
    self.background_color = 'none' if config.background_image.present?
  end

  # Render the text in the image
  canvas.annotate(Magick::Draw.new, 0, 0, 0, 0, code) {
    self.gravity     = Magick::CenterGravity
    self.font        = config.font
    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)


  # Combine images if background image is present
  if config.background_image.present?
    background = Magick::Image.read(config.background_image).first
    background.composite!(canvas, Magick::CenterGravity, Magick::OverCompositeOp)

    image = background.to_blob { self.format = 'PNG' }
  else
    image = canvas.to_blob { self.format = 'PNG' }
  end

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

  canvas.destroy!
  image
end

#sketch?Boolean

:nodoc:

Returns:

  • (Boolean)


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

def sketch? #:nodoc:
  @sketch
end

#wave?Boolean

:nodoc:

Returns:

  • (Boolean)


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

def wave? #:nodoc:
  @wave
end