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]
if color.index(color.min) == preview_color.index(preview_color.min) &&
color.index(color.max) == preview_color.index(preview_color.max)
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"
|