Rewrite Captcha.create to void complex method

This commit is contained in:
Jason Lee 2016-11-08 11:18:07 +08:00
parent 6f26819f5e
commit 41d099022d
2 changed files with 95 additions and 47 deletions

View File

@ -3,6 +3,7 @@ require 'open3'
module RuCaptcha module RuCaptcha
class Captcha class Captcha
class << self class << self
# Genrate ranom RGB color
def random_color def random_color
if RuCaptcha.config.style == :colorful if RuCaptcha.config.style == :colorful
color1 = rand(56) + 15 color1 = rand(56) + 15
@ -16,32 +17,84 @@ module RuCaptcha
end end
end end
# Genrate random Captcha code
def random_chars def random_chars
chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase
chars.gsub!(/[0ol1]/i, (rand(8) + 2).to_s) chars.gsub!(/[0ol1]/i, (rand(8) + 2).to_s)
chars chars
end end
def rand_line_top(text_top, font_size)
text_top + rand(font_size * 0.7).to_i
end
# Create Captcha image by code # Create Captcha image by code
def create(code) def create(code)
chars = code.split('') chars = code.split('')
all_left = 20 full_width = RuCaptcha.config.font_size * chars.size
font_size = RuCaptcha.config.font_size full_height = RuCaptcha.config.font_size
full_height = font_size
full_width = font_size * chars.size
size = "#{full_width}x#{full_height}" size = "#{full_width}x#{full_height}"
return convert_for_windows(size, code) if Gem.win_platform?
opts = command_line_opts(chars, full_width)
convert(size, opts)
end
private
def command_line_opts(chars, full_width)
font_size = RuCaptcha.config.font_size
all_left = 20
half_width = full_width / 2 half_width = full_width / 2
text_top = 0 text_top = 0
text_left = 0 - (font_size * 0.28).to_i 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_width = font_size + text_left
text_opts = []
line_opts = []
opts = { text: [], line: [] }
rgbs = uniq_rgbs_for_each_chars(chars)
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)"
opts[:text] << %(-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)
opts[:line] << %(-draw 'stroke #{line_color} line #{rand(10)},#{left_y} #{right_x},#{right_y}')
end
opts
end
def convert(size, opts)
stroke_width = (RuCaptcha.config.font_size * 0.05).to_i + 1
command = <<-CODE
convert -size #{size} \
-strokewidth #{stroke_width} \
#{opts[:line].join(' ')} \
-pointsize #{RuCaptcha.config.font_size} -weight 500 \
#{opts[:text].join(' ')} \
-wave #{rand(2) + 3}x#{rand(2) + 1} \
-rotate #{rand(10) - 5} \
-gravity NorthWest -sketch 1x10+#{rand(2)} \
-fill none \
-implode #{RuCaptcha.config.implode} -trim label:- png:-
CODE
command.strip!
out, err, _st = Open3.capture3(command)
warn " RuCaptcha #{err.strip}" if err.present?
out
end
# Generate a simple captcha image for Windows Platform
def convert_for_windows(size, code)
png_file_path = Rails.root.join('tmp', 'cache', "#{code}.png")
command = "convert -size #{size} xc:White -gravity Center -weight 12 -pointsize 20 -annotate 0 \"#{code}\" -trim #{png_file_path}"
_out, err, _st = Open3.capture3(command)
warn " RuCaptcha #{err.strip}" if err.present?
png_file_path
end
# Geneate a uniq rgba colors for each chars
def uniq_rgbs_for_each_chars(chars)
rgbs = [] rgbs = []
chars.count.times do |i| chars.count.times do |i|
color = random_color color = random_color
@ -56,43 +109,11 @@ module RuCaptcha
end end
rgbs << color rgbs << color
end end
rgbs
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 end
command = <<-CODE def rand_line_top(text_top, font_size)
convert -size #{size} \ text_top + rand(font_size * 0.7).to_i
-strokewidth #{stroke_width} \
#{line_opts.join(' ')} \
-pointsize #{font_size} -weight 500 \
#{text_opts.join(' ')} \
-wave #{rand(2) + 3}x#{rand(2) + 1} \
-rotate #{rand(10) - 5} \
-gravity NorthWest -sketch 1x10+#{rand(2)} \
-fill none \
-implode #{RuCaptcha.config.implode} -trim label:- png:-
CODE
if Gem.win_platform?
png_file_path = Rails.root.join('tmp', 'cache', "#{code}.png")
command = "convert -size #{size} xc:White -gravity Center -weight 12 -pointsize 20 -annotate 0 \"#{code}\" -trim #{png_file_path}"
out, err, _st = Open3.capture3(command)
warn " RuCaptcha #{err.strip}" if err.present?
png_file_path
else
command.strip!
out, err, _st = Open3.capture3(command)
warn " RuCaptcha #{err.strip}" if err.present?
out
end
end end
def warn(msg) def warn(msg)

View File

@ -34,4 +34,31 @@ describe RuCaptcha::Captcha do
expect(colors).not_to eq colors1 expect(colors).not_to eq colors1
end end
end end
describe '.rand_line_top' do
it 'should work' do
expect(RuCaptcha::Captcha.send(:rand_line_top, 1, 24)).to be_a(Integer)
end
end
describe '.uniq_rgbs_for_each_chars' do
let(:chars) { %w(a b c d e) }
let(:colors) { RuCaptcha::Captcha.send(:uniq_rgbs_for_each_chars, chars) }
it 'should work' do
expect(colors.length).to eq chars.length
expect(colors[0].length).to eq 3
end
it 'Be sure the color not same as preview color' do
pre_rgb = nil
colors.each do |rgb|
if pre_rgb
same = rgb.index(rgb.min) == pre_rgb.index(rgb.min) && rgb.index(rgb.max) == pre_rgb.index(pre_rgb.max)
expect(same).not_to eq true
end
pre_rgb = rgb
end
end
end
end end