Class: EasyCaptcha::Generator::Default
- Defined in:
- lib/easy_captcha/generator/default.rb
Overview
default generator
Instance Attribute Summary collapse
-
#blur ⇒ Object
Gaussian Blur.
-
#blur_radius ⇒ Object
Gaussian Blur.
-
#blur_sigma ⇒ Object
Gaussian Blur.
-
#font_family ⇒ Object
Font.
-
#font_fill_color ⇒ Object
Font.
-
#font_size ⇒ Object
Font.
-
#font_stroke ⇒ Object
Font.
-
#font_stroke_color ⇒ Object
Font.
-
#image_background_color ⇒ Object
Background.
-
#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
-
#blur? ⇒ Boolean
:nodoc:.
-
#defaults ⇒ Object
set default values.
-
#generate(code) ⇒ Object
generate image.
-
#sketch? ⇒ Boolean
:nodoc:.
-
#wave? ⇒ Boolean
:nodoc:.
Methods inherited from Base
Constructor Details
This class inherits a constructor from EasyCaptcha::Generator::Base
Instance Attribute Details
#blur ⇒ Object
Gaussian Blur
44 45 46 |
# File 'lib/easy_captcha/generator/default.rb', line 44 def blur @blur end |
#blur_radius ⇒ Object
Gaussian Blur
44 45 46 |
# File 'lib/easy_captcha/generator/default.rb', line 44 def blur_radius @blur_radius end |
#blur_sigma ⇒ Object
Gaussian Blur
44 45 46 |
# File 'lib/easy_captcha/generator/default.rb', line 44 def blur_sigma @blur_sigma end |
#font_family ⇒ Object
Font
29 30 31 |
# File 'lib/easy_captcha/generator/default.rb', line 29 def font_family @font_family end |
#font_fill_color ⇒ Object
Font
29 30 31 |
# File 'lib/easy_captcha/generator/default.rb', line 29 def font_fill_color @font_fill_color end |
#font_size ⇒ Object
Font
29 30 31 |
# File 'lib/easy_captcha/generator/default.rb', line 29 def font_size @font_size end |
#font_stroke ⇒ Object
Font
29 30 31 |
# File 'lib/easy_captcha/generator/default.rb', line 29 def font_stroke @font_stroke end |
#font_stroke_color ⇒ Object
Font
29 30 31 |
# File 'lib/easy_captcha/generator/default.rb', line 29 def font_stroke_color @font_stroke_color end |
#image_background_color ⇒ Object
Background
32 33 34 |
# File 'lib/easy_captcha/generator/default.rb', line 32 def image_background_color @image_background_color end |
#implode ⇒ Object
Implode
41 42 43 |
# File 'lib/easy_captcha/generator/default.rb', line 41 def implode @implode end |
#sketch ⇒ Object
Sketch
35 36 37 |
# File 'lib/easy_captcha/generator/default.rb', line 35 def sketch @sketch end |
#sketch_radius ⇒ Object
Sketch
35 36 37 |
# File 'lib/easy_captcha/generator/default.rb', line 35 def sketch_radius @sketch_radius end |
#sketch_sigma ⇒ Object
Sketch
35 36 37 |
# File 'lib/easy_captcha/generator/default.rb', line 35 def sketch_sigma @sketch_sigma end |
#wave ⇒ Object
Wave
38 39 40 |
# File 'lib/easy_captcha/generator/default.rb', line 38 def wave @wave end |
#wave_amplitude ⇒ Object
Wave
38 39 40 |
# File 'lib/easy_captcha/generator/default.rb', line 38 def wave_amplitude @wave_amplitude end |
#wave_length ⇒ Object
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:
54 55 56 |
# File 'lib/easy_captcha/generator/default.rb', line 54 def blur? #:nodoc: @blur end |
#defaults ⇒ Object
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.('../../../../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:
46 47 48 |
# File 'lib/easy_captcha/generator/default.rb', line 46 def sketch? #:nodoc: @sketch end |
#wave? ⇒ Boolean
:nodoc:
50 51 52 |
# File 'lib/easy_captcha/generator/default.rb', line 50 def wave? #:nodoc: @wave end |