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



30
31
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
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rucaptcha/captcha.rb', line 30

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    = []

  rgbs = []
  chars.count.times do |i|
    color = random_color
    if i > 0
      preview_color = rgbs[i - 1]
      # Avoid color same as preview color
      if color.index(color.min) == preview_color.index(preview_color.min) &&
         color.index(color.max) == preview_color.index(preview_color.max)
        # adjust RGB order
        color = [color[1], color[2], color[0]]
      end
    end
    rgbs << color
  end

  chars.each_with_index do |char, i|
    rgb = RuCaptcha.config.style == :colorful ? rgbs[i] : rgbs[0]
    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



25
26
27
# File 'lib/rucaptcha/captcha.rb', line 25

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

.random_charsObject



19
20
21
22
23
# File 'lib/rucaptcha/captcha.rb', line 19

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
16
17
# File 'lib/rucaptcha/captcha.rb', line 6

def random_color
  if RuCaptcha.config.style == :colorful
    color1 = rand(56) + 15
    color2 = rand(10) + 140
    color = [color1, color2, rand(15)]
    color.shuffle!
    color
  else
    color_seed = rand(40) + 10
    [color_seed, color_seed, color_seed]
  end
end

.warn(msg) ⇒ Object



98
99
100
101
# File 'lib/rucaptcha/captcha.rb', line 98

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