Class: RuCaptcha::Captcha

Inherits:
Object
  • Object
show all
Defined in:
lib/rucaptcha/captcha.rb

Class Method Summary collapse

Class Method Details

.create(code) ⇒ Object

Create Captcha image by code



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
# File 'lib/rucaptcha/captcha.rb', line 32

def create(code)
  chars        = code.split('')
  all_left     = 20
  font_size    = RuCaptcha.config.font_size
  full_height  = font_size
  full_width   = font_size * chars.size
  size         = "#{full_width}x#{full_height}"
  half_width   = full_width / 2
  text_top     = 0
  text_left    = 0 - (font_size * 0.28).to_i
  stroke_width = (font_size * 0.05).to_i + 1
  text_width   = font_size + text_left
  text_opts    = []
  line_opts    = []

  chars.each_with_index do |char, i|
    rgb = random_color
    text_color = "rgba(#{rgb.join(',')}, 1)"
    line_color = "rgba(#{rgb.join(',')}, 0.6)"
    text_opts << %(-fill '#{text_color}' -draw 'text #{(text_left + text_width) * i + all_left},#{text_top} "#{char}"')
    left_y = rand_line_top(text_top, font_size)
    right_x = half_width + (half_width * 0.3).to_i
    right_y = rand_line_top(text_top, font_size)
    line_opts << %(-draw 'stroke #{line_color} line #{rand(10)},#{left_y} #{right_x},#{right_y}')
  end

  command = "    convert -size \#{size} \\\n    -strokewidth \#{stroke_width} \\\n    \#{line_opts.join(' ')} \\\n    -pointsize \#{font_size} -weight 500 \\\n    \#{text_opts.join(' ')}  \\\n    -wave \#{rand(2) + 3}x\#{rand(2) + 1} \\\n    -rotate \#{rand(10) - 5} \\\n    -gravity NorthWest -sketch 1x10+\#{rand(2)} \\\n    -fill none \\\n    -implode \#{RuCaptcha.config.implode} -trim label:- png:-\n  CODE\n\n  if Gem.win_platform?\n    png_file_path = Rails.root.join('tmp', 'cache', \"\#{code}.png\")\n    command = \"convert -size \#{size} xc:White -gravity Center -weight 12 -pointsize 20 -annotate 0 \\\"\#{code}\\\" -trim \#{png_file_path}\"\n    out, err, _st = Open3.capture3(command)\n    warn \"  RuCaptcha \#{err.strip}\" if err.present?\n    png_file_path\n  else\n    command.strip!\n    out, err, _st = Open3.capture3(command)\n    warn \"  RuCaptcha \#{err.strip}\" if err.present?\n    out\n  end\nend\n"

.rand_line_top(text_top, font_size) ⇒ Object



27
28
29
# File 'lib/rucaptcha/captcha.rb', line 27

def rand_line_top(text_top, font_size)
  text_top + rand(font_size * 0.7).to_i
end

.random_charsObject



21
22
23
24
25
# File 'lib/rucaptcha/captcha.rb', line 21

def random_chars
  chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase
  chars.gsub!(/[0ol1]/i, (rand(8) + 2).to_s)
  chars
end

.random_colorObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/rucaptcha/captcha.rb', line 6

def random_color
  if RuCaptcha.config.style == :colorful
    color = [random_color_seed, random_color_seed, random_color_seed]
    color[rand(3)] = 0.to_s(8)
    color
  else
    color_seed = rand(50).to_s(8)
    [color_seed, color_seed, color_seed]
  end
end

.random_color_seedObject



17
18
19
# File 'lib/rucaptcha/captcha.rb', line 17

def random_color_seed
  (rand(150) + 10).to_s(8)
end

.warn(msg) ⇒ Object



85
86
87
88
# File 'lib/rucaptcha/captcha.rb', line 85

def warn(msg)
  msg = "  RuCaptcha #{msg}"
  Rails.logger.error(msg)
end