Class: SimpleCaptchaReloaded::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_captcha_reloaded/image.rb

Constant Summary collapse

IMAGE_STYLES =
{
  'embosed_silver'  => ['-fill darkblue', '-shade 20x60', '-background white'],
  'simply_red'      => ['-fill darkred', '-background white'],
  'simply_green'    => ['-fill darkgreen', '-background white'],
  'simply_blue'     => ['-fill darkblue', '-background white'],
  'distorted_black' => ['-fill darkblue', '-edge 10', '-background white'],
  'all_black'       => ['-fill darkblue', '-edge 2', '-background white'],
  'charcoal_grey'   => ['-fill darkblue', '-charcoal 5', '-background white'],
  'almost_invisible' => ['-fill red', '-solarize 50', '-background white']
}
DISTORTIONS =
{
  low:    -> { [0 + rand(2), 80 + rand(20)] },
  medium: -> { [2 + rand(2), 50 + rand(20)] },
  high:   -> { [4 + rand(2), 30 + rand(20)] },
}
IMPLODES =
{
  none:    0,
  low:     0.1,
  medium:  0.2,
  high:    0.3
}

Instance Method Summary collapse

Constructor Details

#initialize(implode: :medium, distortion: :medium, image_styles: IMAGE_STYLES.slice('simply_red', 'simply_green', 'simply_blue'), noise: 1, size: '100x28', image_magick_path: '') ⇒ Image

Returns a new instance of Image.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/simple_captcha_reloaded/image.rb', line 30

def initialize(implode: :medium,
               distortion: :medium,
               image_styles: IMAGE_STYLES.slice('simply_red', 'simply_green', 'simply_blue'),
               noise: 1,
               size: '100x28',
               image_magick_path: '')

  @implode           = implode
  @distortion        = distortion
  if !DISTORTIONS.keys.include?(@distortion)
    @distortion = DISTORTIONS.keys.sample
  end
  @distortion_function = DISTORTIONS[@distortion]
  @image_styles      = image_styles
  @noise             = noise
  @size              = size
  @image_magick_path = image_magick_path
end

Instance Method Details

#generate(text) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/simple_captcha_reloaded/image.rb', line 49

def generate(text)
  amplitude, frequency = calculate_distortion

  params = image_params.dup
  params << "-size #{@size}"
  params << "-wave #{amplitude}x#{frequency}"
  params << "-gravity Center"
  params << "-pointsize 22"
  params << "-implode #{IMPLODES[@implode]}"
  params << "label:#{text}"
  params << "-evaluate Uniform-noise #{@noise}"
  params << "jpeg:-"
  run("convert", params: params.join(' '))
end