Class: EasyCaptcha::Generator::Default
- 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.('../../../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
-
#background_color ⇒ Object
Background.
-
#background_fill ⇒ Object
Background.
-
#background_image ⇒ Object
Background.
-
#blur ⇒ Object
Blur.
-
#blur_radius ⇒ Object
Blur.
-
#blur_sigma ⇒ Object
Blur.
-
#code ⇒ Object
readonly
The CAPTCHA code.
-
#font ⇒ Object
Font.
-
#font_family ⇒ Object
Font.
-
#font_fill_color ⇒ Object
Font.
-
#font_size ⇒ Object
Font.
-
#font_stroke ⇒ Object
Font.
-
#font_stroke_color ⇒ Object
Font.
-
#image ⇒ Object
readonly
The CAPTCHA image.
-
#implode ⇒ Object
Implode.
-
#sketch ⇒ Object
Sketch.
-
#sketch_radius ⇒ Object
Sketch.
-
#sketch_sigma ⇒ Object
Sketch.
-
#wave ⇒ Object
Wave.
-
#wave_amplitude ⇒ Object
Wave.
-
#wave_length ⇒ Object
Wave.
Instance Method Summary collapse
- #apply_effects ⇒ Object
-
#blur? ⇒ Boolean
:nodoc:.
- #create_blob ⇒ Object
- #create_composite_blob ⇒ Object
-
#defaults ⇒ Object
set default values.
- #free_canvas ⇒ Object
-
#generate(code) ⇒ Object
generate image.
- #image_background_color ⇒ Object
- #image_background_color=(val) ⇒ Object
- #set_image_encoding ⇒ Object
-
#sketch? ⇒ Boolean
:nodoc:.
-
#wave? ⇒ Boolean
:nodoc:.
Methods inherited from Base
Constructor Details
This class inherits a constructor from EasyCaptcha::Generator::Base
Instance Attribute Details
#background_color ⇒ Object
Background
41 42 43 |
# File 'lib/easy_captcha/generator/default.rb', line 41 def background_color @background_color end |
#background_fill ⇒ Object
Background
41 42 43 |
# File 'lib/easy_captcha/generator/default.rb', line 41 def background_fill @background_fill end |
#background_image ⇒ Object
Background
41 42 43 |
# File 'lib/easy_captcha/generator/default.rb', line 41 def background_image @background_image end |
#blur ⇒ Object
Blur
44 45 46 |
# File 'lib/easy_captcha/generator/default.rb', line 44 def blur @blur end |
#blur_radius ⇒ Object
Blur
44 45 46 |
# File 'lib/easy_captcha/generator/default.rb', line 44 def blur_radius @blur_radius end |
#blur_sigma ⇒ Object
Blur
44 45 46 |
# File 'lib/easy_captcha/generator/default.rb', line 44 def blur_sigma @blur_sigma end |
#code ⇒ Object (readonly)
The CAPTCHA code
59 60 61 |
# File 'lib/easy_captcha/generator/default.rb', line 59 def code @code end |
#font ⇒ Object
Font
47 48 49 |
# File 'lib/easy_captcha/generator/default.rb', line 47 def font @font end |
#font_family ⇒ Object
Font
47 48 49 |
# File 'lib/easy_captcha/generator/default.rb', line 47 def font_family @font_family end |
#font_fill_color ⇒ Object
Font
47 48 49 |
# File 'lib/easy_captcha/generator/default.rb', line 47 def font_fill_color @font_fill_color end |
#font_size ⇒ Object
Font
47 48 49 |
# File 'lib/easy_captcha/generator/default.rb', line 47 def font_size @font_size end |
#font_stroke ⇒ Object
Font
47 48 49 |
# File 'lib/easy_captcha/generator/default.rb', line 47 def font_stroke @font_stroke end |
#font_stroke_color ⇒ Object
Font
47 48 49 |
# File 'lib/easy_captcha/generator/default.rb', line 47 def font_stroke_color @font_stroke_color end |
#image ⇒ Object (readonly)
The CAPTCHA image
62 63 64 |
# File 'lib/easy_captcha/generator/default.rb', line 62 def image @image end |
#implode ⇒ Object
Implode
50 51 52 |
# File 'lib/easy_captcha/generator/default.rb', line 50 def implode @implode end |
#sketch ⇒ Object
Sketch
53 54 55 |
# File 'lib/easy_captcha/generator/default.rb', line 53 def sketch @sketch end |
#sketch_radius ⇒ Object
Sketch
53 54 55 |
# File 'lib/easy_captcha/generator/default.rb', line 53 def sketch_radius @sketch_radius end |
#sketch_sigma ⇒ Object
Sketch
53 54 55 |
# File 'lib/easy_captcha/generator/default.rb', line 53 def sketch_sigma @sketch_sigma end |
#wave ⇒ Object
Wave
56 57 58 |
# File 'lib/easy_captcha/generator/default.rb', line 56 def wave @wave end |
#wave_amplitude ⇒ Object
Wave
56 57 58 |
# File 'lib/easy_captcha/generator/default.rb', line 56 def wave_amplitude @wave_amplitude end |
#wave_length ⇒ Object
Wave
56 57 58 |
# File 'lib/easy_captcha/generator/default.rb', line 56 def wave_length @wave_length end |
Instance Method Details
#apply_effects ⇒ Object
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:
76 77 78 |
# File 'lib/easy_captcha/generator/default.rb', line 76 def blur? #:nodoc: @blur end |
#create_blob ⇒ Object
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_blob ⇒ Object
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 |
#defaults ⇒ Object
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_canvas ⇒ Object
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_color ⇒ Object
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_encoding ⇒ Object
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:
80 81 82 |
# File 'lib/easy_captcha/generator/default.rb', line 80 def sketch? #:nodoc: @sketch end |
#wave? ⇒ Boolean
:nodoc:
84 85 86 |
# File 'lib/easy_captcha/generator/default.rb', line 84 def wave? #:nodoc: @wave end |