Class: EasyCaptcha::Generator::Default
- Defined in:
- lib/easy_captcha/generator/default.rb
Overview
default generator
Instance Attribute Summary collapse
-
#background_image ⇒ Object
Background.
-
#blur ⇒ Object
Gaussian Blur.
-
#blur_radius ⇒ Object
Gaussian Blur.
-
#blur_sigma ⇒ Object
Gaussian Blur.
-
#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_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
#background_image ⇒ Object
Background
33 34 35 |
# File 'lib/easy_captcha/generator/default.rb', line 33 def background_image @background_image end |
#blur ⇒ Object
Gaussian Blur
45 46 47 |
# File 'lib/easy_captcha/generator/default.rb', line 45 def blur @blur end |
#blur_radius ⇒ Object
Gaussian Blur
45 46 47 |
# File 'lib/easy_captcha/generator/default.rb', line 45 def blur_radius @blur_radius end |
#blur_sigma ⇒ Object
Gaussian Blur
45 46 47 |
# File 'lib/easy_captcha/generator/default.rb', line 45 def blur_sigma @blur_sigma end |
#font ⇒ Object
Font
30 31 32 |
# File 'lib/easy_captcha/generator/default.rb', line 30 def font @font end |
#font_family ⇒ Object
Font
30 31 32 |
# File 'lib/easy_captcha/generator/default.rb', line 30 def font_family @font_family end |
#font_fill_color ⇒ Object
Font
30 31 32 |
# File 'lib/easy_captcha/generator/default.rb', line 30 def font_fill_color @font_fill_color end |
#font_size ⇒ Object
Font
30 31 32 |
# File 'lib/easy_captcha/generator/default.rb', line 30 def font_size @font_size end |
#font_stroke ⇒ Object
Font
30 31 32 |
# File 'lib/easy_captcha/generator/default.rb', line 30 def font_stroke @font_stroke end |
#font_stroke_color ⇒ Object
Font
30 31 32 |
# File 'lib/easy_captcha/generator/default.rb', line 30 def font_stroke_color @font_stroke_color end |
#image_background_color ⇒ Object
Background
33 34 35 |
# File 'lib/easy_captcha/generator/default.rb', line 33 def image_background_color @image_background_color end |
#implode ⇒ Object
Implode
42 43 44 |
# File 'lib/easy_captcha/generator/default.rb', line 42 def implode @implode end |
#sketch ⇒ Object
Sketch
36 37 38 |
# File 'lib/easy_captcha/generator/default.rb', line 36 def sketch @sketch end |
#sketch_radius ⇒ Object
Sketch
36 37 38 |
# File 'lib/easy_captcha/generator/default.rb', line 36 def sketch_radius @sketch_radius end |
#sketch_sigma ⇒ Object
Sketch
36 37 38 |
# File 'lib/easy_captcha/generator/default.rb', line 36 def sketch_sigma @sketch_sigma end |
#wave ⇒ Object
Wave
39 40 41 |
# File 'lib/easy_captcha/generator/default.rb', line 39 def wave @wave end |
#wave_amplitude ⇒ Object
Wave
39 40 41 |
# File 'lib/easy_captcha/generator/default.rb', line 39 def wave_amplitude @wave_amplitude end |
#wave_length ⇒ Object
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:
55 56 57 |
# File 'lib/easy_captcha/generator/default.rb', line 55 def blur? #:nodoc: @blur end |
#defaults ⇒ Object
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.('../../../../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:
47 48 49 |
# File 'lib/easy_captcha/generator/default.rb', line 47 def sketch? #:nodoc: @sketch end |
#wave? ⇒ Boolean
:nodoc:
51 52 53 |
# File 'lib/easy_captcha/generator/default.rb', line 51 def wave? #:nodoc: @wave end |