class EasyCaptcha::Generator::Default
rubocop:disable Metrics/ClassLength default generator
Constants
- DEFAULT_CONFIG
Attributes
background_color[RW]
Background
background_fill[RW]
Background
background_image[RW]
Background
blur[RW]
Blur
blur_radius[RW]
Blur
blur_sigma[RW]
Blur
code[R]
The CAPTCHA code
font[RW]
Font
font_family[RW]
Font
font_fill_color[RW]
Font
font_size[RW]
Font
font_stroke[RW]
Font
font_stroke_color[RW]
Font
image[R]
The CAPTCHA image
implode[RW]
Implode
sketch[RW]
Sketch
sketch_radius[RW]
Sketch
sketch_sigma[RW]
Sketch
wave[RW]
Wave
wave_amplitude[RW]
Wave
wave_length[RW]
Wave
Public Instance Methods
apply_effects()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 99 def apply_effects apply_sketch apply_implode apply_blur apply_wave apply_crop end
create_blob()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
set default values
# File lib/easy_captcha/generator/default.rb, line 34 def defaults DEFAULT_CONFIG.map do |k, v| send("#{k}=", v) end end
free_canvas()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 139 def free_canvas @canvas.destroy! if canvas.respond_to?('destroy!') end
generate(code)
click to toggle source
generate image
# 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()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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
Private Instance Methods
apply_blur()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 145 def apply_blur return @canvas unless blur? apply_image_blur apply_motion_blur end
apply_crop()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 179 def apply_crop return @canvas unless canvas.respond_to?(:crop) # Crop image because to big after waveing ### # https://rmagick.github.io/image1.html#crop # Parameters: # gravity (Magick::GravityType) - the gravity type # width (Numeric) - width of region # height (Numeric) - height of region @canvas = canvas.crop(Magick::CenterGravity, EasyCaptcha.captcha_image_width, EasyCaptcha.captcha_image_height) end
apply_image_blur()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 151 def apply_image_blur return @canvas unless canvas.respond_to?(:blur_image) ### # https://rmagick.github.io/image1.html#blur_image # Parameters: # radius (Float) (defaults to: 0.0) - The radius of the Gaussian operator. # sigma (Float) (defaults to: 1.0) - The standard deviation of the Gaussian operator. Must be non-zero. @canvas = canvas.blur_image( generator_config.blur_radius, generator_config.blur_sigma ) end
apply_implode()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 191 def apply_implode ### # https://rmagick.github.io/image2.html#implode # Parameters: # amount (Float) (defaults to: 0.50) - The precentage to implode the image @canvas = canvas.implode(generator_config.implode.to_f) if generator_config.implode.is_a? Float end
apply_motion_blur()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 164 def apply_motion_blur return @canvas unless canvas.respond_to?(:motion_blur) ### # https://rmagick.github.io/image2.html#motion_blur # Parameters: # radius (Float) (defaults to: 0.0) - The radius of the Gaussian operator. # sigma (Float) (defaults to: 1.0) - The standard deviation of the Gaussian operator. Must be non-zero. # angle (Float) (defaults to: 0.0) - The angle (in degrees) of the blurring motion. @canvas = canvas.motion_blur( generator_config.blur_radius, generator_config.blur_sigma, rand(180) ) end
apply_sketch()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 199 def apply_sketch return @canvas unless sketch? && canvas.respond_to?(:sketch) ### # https://rmagick.github.io/image3.html#sketch # Parameters: # radius (Float) (defaults to: 0.0) - The radius of the Gaussian operator. # sigma (Float) (defaults to: 1.0) - The standard deviation of the Gaussian operator. # angle (Float) (defaults to: 0.0) - The angle (in degrees) of the sketch lines. @canvas = canvas.sketch(generator_config.sketch_radius, generator_config.sketch_sigma, rand(180)) end
apply_wave()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 210 def apply_wave return @canvas unless wave? && canvas.respond_to?(:wave) ### # https://rmagick.github.io/image3.html#wave # Parameters: # amplitude (Float) (defaults to: 25.0) - the amplitude # wavelength (Float) (defaults to: 150.0) - the wave length @canvas = canvas.wave(random_wave_amplitude, random_wave_length) end
canvas()
click to toggle source
rubocop:disable Metrics/AbcSize
# File lib/easy_captcha/generator/default.rb, line 221 def canvas config = generator_config @canvas = nil if @canvas.respond_to?('destroyed?') && @canvas.destroyed? ### # https://rmagick.github.io/image1.html#new # Parameters: # cols (Numeric) - the image width # rows (Numeric) - the image height # fill (Magick::GradientFill, Magick::HatchFill, Magick::TextureFill) (defaults to: nil) # - if object is given as fill argument, background color will be filled using it. @canvas ||= Magick::Image.new( EasyCaptcha.captcha_image_width, EasyCaptcha.captcha_image_height, config.background_fill ) do |image| image.background_color = config.background_color unless config.background_color.nil? image.background_color = 'none' if config.background_image.present? || config.background_fill.present? end end
generator_config()
click to toggle source
rubocop:enable Metrics/AbcSize
# File lib/easy_captcha/generator/default.rb, line 242 def generator_config @generator_config ||= self end
random_wave_amplitude()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 246 def random_wave_amplitude rand( generator_config.wave_amplitude.last - generator_config.wave_amplitude.first ) + generator_config.wave_amplitude.first end
random_wave_length()
click to toggle source
# File lib/easy_captcha/generator/default.rb, line 252 def random_wave_length rand( generator_config.wave_length.last - generator_config.wave_length.first ) + generator_config.wave_length.first end
render_code_in_image()
click to toggle source
rubocop:disable Metrics/AbcSize
# File lib/easy_captcha/generator/default.rb, line 259 def render_code_in_image config = generator_config # Render the text in the image Magick::Draw.new.annotate(canvas, 0, 0, 0, 0, code.gsub(/%/, '\%')) do 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.positive? self.stroke = config.font_stroke_color self.stroke_width = config.font_stroke end self.pointsize = config.font_size end end