Class: EasyCaptcha::Generator::Default

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

Overview

rubocop:disable Metrics/ClassLength default generator

Constant Summary collapse

DEFAULT_CONFIG =
{
  background_color: '#FFFFFF',
  background_fill: nil,
  background_image: nil,
  blur: true,
  blur_radius: 1,
  blur_sigma: 2,
  font: File.expand_path('../../../resources/captcha.ttf', __dir__),
  font_fill_color: '#333333',
  font_size: 24,
  font_stroke: 0,
  font_stroke_color: '#000000',
  implode: 0.05,
  sketch: true,
  sketch_radius: 3,
  sketch_sigma: 1,
  wave: true,
  wave_amplitude: (3..5),
  wave_length: (60..100)
}.freeze

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_colorObject

Background



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

def background_color
  @background_color
end

#background_fillObject

Background



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

def background_fill
  @background_fill
end

#background_imageObject

Background



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

def background_image
  @background_image
end

#blurObject

Blur



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

def blur
  @blur
end

#blur_radiusObject

Blur



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

def blur_radius
  @blur_radius
end

#blur_sigmaObject

Blur



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

def blur_sigma
  @blur_sigma
end

#codeObject (readonly)

The CAPTCHA code



59
60
61
# File 'lib/easy_captcha/generator/default.rb', line 59

def code
  @code
end

#fontObject

Font



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

def font
  @font
end

#font_familyObject

Font



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

def font_family
  @font_family
end

#font_fill_colorObject

Font



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

def font_fill_color
  @font_fill_color
end

#font_sizeObject

Font



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

def font_size
  @font_size
end

#font_strokeObject

Font



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

def font_stroke
  @font_stroke
end

#font_stroke_colorObject

Font



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

def font_stroke_color
  @font_stroke_color
end

#imageObject (readonly)

The CAPTCHA image



62
63
64
# File 'lib/easy_captcha/generator/default.rb', line 62

def image
  @image
end

#implodeObject

Implode



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

def implode
  @implode
end

#sketchObject

Sketch



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

def sketch
  @sketch
end

#sketch_radiusObject

Sketch



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

def sketch_radius
  @sketch_radius
end

#sketch_sigmaObject

Sketch



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

def sketch_sigma
  @sketch_sigma
end

#waveObject

Wave



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

def wave
  @wave
end

#wave_amplitudeObject

Wave



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

def wave_amplitude
  @wave_amplitude
end

#wave_lengthObject

Wave



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

def wave_length
  @wave_length
end

Instance Method Details

#apply_effectsObject



99
100
101
102
103
104
105
# File 'lib/easy_captcha/generator/default.rb', line 99

def apply_effects
  apply_sketch
  apply_implode
  apply_blur
  apply_wave
  apply_crop
end

#blur?Boolean

:nodoc:

Returns:

  • (Boolean)


76
77
78
# File 'lib/easy_captcha/generator/default.rb', line 76

def blur? #:nodoc:
  @blur
end

#create_blobObject



107
108
109
110
111
112
113
# File 'lib/easy_captcha/generator/default.rb', line 107

def create_blob
  @image =  if generator_config.background_image.present?
              create_composite_blob
            else
              canvas.to_blob { self.format = 'PNG' }
            end
end

#create_composite_blobObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/easy_captcha/generator/default.rb', line 115

def create_composite_blob
  # Background Random Position
  gravity = [
    Magick::CenterGravity,
    Magick::EastGravity,
    Magick::NorthEastGravity,
    Magick::NorthGravity,
    Magick::NorthWestGravity,
    Magick::SouthGravity,
    Magick::SouthEastGravity,
    Magick::SouthWestGravity,
    Magick::WestGravity
  ].sample

  background = Magick::Image.read(generator_config.background_image).first
  background.composite!(canvas, gravity, Magick::OverCompositeOp)
  background = background.crop(gravity, EasyCaptcha.captcha_image_width, EasyCaptcha.captcha_image_height)
  background.to_blob { self.format = 'PNG' }
end

#defaultsObject

set default values



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

def defaults
  DEFAULT_CONFIG.map do |k, v|
    send("#{k}=", v)
  end
end

#free_canvasObject



139
140
141
# File 'lib/easy_captcha/generator/default.rb', line 139

def free_canvas
  @canvas.destroy! if canvas.respond_to?('destroy!')
end

#generate(code) ⇒ Object

generate image



89
90
91
92
93
94
95
96
97
# File 'lib/easy_captcha/generator/default.rb', line 89

def generate(code)
  @code = code
  render_code_in_image
  apply_effects
  create_blob
  set_image_encoding
  free_canvas
  @image
end

#image_background_colorObject



70
71
72
73
74
# File 'lib/easy_captcha/generator/default.rb', line 70

def image_background_color
  warn  '[DEPRECATION] EasyCaptcha configuration option `image_background_color` is deprecated. ' \
        'Please use `background_color` instead.'
  background_color
end

#image_background_color=(val) ⇒ Object



64
65
66
67
68
# File 'lib/easy_captcha/generator/default.rb', line 64

def image_background_color=(val)
  warn  '[DEPRECATION] EasyCaptcha configuration option `image_background_color` is deprecated. ' \
        'Please use `background_color` instead.'
  self.background_color = val
end

#set_image_encodingObject



135
136
137
# File 'lib/easy_captcha/generator/default.rb', line 135

def set_image_encoding
  @image = image.force_encoding 'UTF-8' if image.respond_to? :force_encoding
end

#sketch?Boolean

:nodoc:

Returns:

  • (Boolean)


80
81
82
# File 'lib/easy_captcha/generator/default.rb', line 80

def sketch? #:nodoc:
  @sketch
end

#wave?Boolean

:nodoc:

Returns:

  • (Boolean)


84
85
86
# File 'lib/easy_captcha/generator/default.rb', line 84

def wave? #:nodoc:
  @wave
end