From 3be8125c40f78804a0068e6faa7e49f17f49d6f1 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 26 Oct 2015 17:40:59 +0800 Subject: [PATCH] Update Image style and colors, and fix Captcha class missing bug. - No case sensitive; - Export config.implode; - Improve image color and style; - Don't generate chars in 'l,o,0,1'. - Render lower case chars on image. --- CHANGELOG.md | 11 +++++- README.md | 5 +-- lib/rucaptcha.rb | 5 +-- lib/rucaptcha/captcha.rb | 53 ++++++++++++++++++++--------- lib/rucaptcha/configuration.rb | 2 +- lib/rucaptcha/controller_helpers.rb | 13 +++++-- lib/rucaptcha/version.rb | 2 +- rucaptcha.gemspec | 3 +- 8 files changed, 64 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d32489a..6d0637f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,13 @@ -0.0.1 +0.1.2 +----- + +- No case sensitive; +- Export config.implode; +- Improve image color and style; +- Don't generate chars in 'l,o,0,1'. +- Render lower case chars on image. + +0.1.1 ----- - Include default validation I18n messages (en, zh-CN, zh-TW). diff --git a/README.md b/README.md index 19e69aa..702d030 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # RuCaptcha -This is a Captcha gem for Rails Application. It base on [mini_magick](https://github.com/minimagick/minimagick) to run ImageMagick command to draw Captcha image. +This is a Captcha gem for Rails Application. It run ImageMagick command to draw Captcha image. So it's NO performance issue, and memory leak issue. @@ -10,7 +10,6 @@ Idea by: https://ruby-china.org/topics/20558#reply4 ### Requirements - ImageMagick -- [mini_magick](https://github.com/minimagick/minimagick) ### Usage @@ -30,8 +29,6 @@ RuCaptcha.configure do self.width = 180 # Image height, default: 48 self.height = 48 - # Font size, default: 38 - self.font_size = 38 end ``` diff --git a/lib/rucaptcha.rb b/lib/rucaptcha.rb index 34f8167..b65ce13 100644 --- a/lib/rucaptcha.rb +++ b/lib/rucaptcha.rb @@ -3,6 +3,7 @@ require_relative 'rucaptcha/version' require_relative 'rucaptcha/configuration' require_relative 'rucaptcha/controller_helpers' require_relative 'rucaptcha/view_helpers' +require_relative 'rucaptcha/captcha' require_relative 'rucaptcha/engine' module RuCaptcha @@ -11,9 +12,9 @@ module RuCaptcha return @config if defined?(@config) @config = Configuration.new @config.len = 4 - @config.width = 180 + @config.width = 150 @config.height = 48 - @config.font_size = 38 + @config.implode = 0.4 @config end diff --git a/lib/rucaptcha/captcha.rb b/lib/rucaptcha/captcha.rb index 42ac59c..b5a6497 100644 --- a/lib/rucaptcha/captcha.rb +++ b/lib/rucaptcha/captcha.rb @@ -2,23 +2,44 @@ require 'posix-spawn' module RuCaptcha class Captcha - class << self - def create(code) - color = "rgba(#{rand(100)},#{rand(100)},#{rand(100)}, 1)" - size = "#{RuCaptcha.config.width}x#{RuCaptcha.config.height}" - command = <<-CODE - convert -size #{size} -fill "#{color}" -background white \ - -draw 'stroke #{color} line #{rand(20)},#{rand(28)} #{rand(30) + 10},#{rand(48)}' \ - -draw 'stroke #{color} line #{rand(50)},#{rand(28)} #{rand(180)},#{rand(48)}' \ - -wave #{2 + rand(2)}x#{80 + rand(20)} \ - -gravity Center -sketch 2x19+#{rand(6)} -pointsize #{RuCaptcha.config.font_size} -implode 0.3 label:#{code.upcase} png:- - CODE - command.strip! - # puts command - pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command) - Process.waitpid(pid) - stdout.read + def self.rand_color + r = rand(129).to_s(8).to_i + rgb = [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)] + + "rgba(#{rgb.join(',')},1)" + end + + def self.create(code) + size = "#{RuCaptcha.config.width}x#{RuCaptcha.config.height}" + font_size = (RuCaptcha.config.height * 0.8).to_i + half_width = RuCaptcha.config.width / 2 + half_height = RuCaptcha.config.height / 2 + line_color = rand_color + + chars = code.split('') + text_opts = [] + text_top = (RuCaptcha.config.height - font_size) / 2 + text_padding = 5 + text_width = (RuCaptcha.config.width / chars.size) - text_padding * 2 + text_left = 5 + chars.each_with_index do |char, i| + text_opts << %(-fill '#{rand_color}' -draw 'text #{(text_left + text_width) * i + text_left},#{text_top} "#{char}"') end + + command = <<-CODE + convert -size #{size} #{text_opts.join(' ')} \ + -draw 'stroke #{line_color} line #{rand(10)},#{rand(20)} #{half_width + rand(half_width)},#{rand(half_height)}' \ + -draw 'stroke #{line_color} line #{rand(10)},#{rand(25)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \ + -draw 'stroke #{line_color} line #{rand(10)},#{rand(30)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \ + -wave #{rand(2) + 2}x#{rand(2) + 1} \ + -gravity NorthWest -sketch 1x10+#{rand(1)} -pointsize #{font_size} -weight 700 \ + -implode #{RuCaptcha.config.implode} label:- png:- + CODE + command.strip! + # puts command + pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command) + Process.waitpid(pid) + stdout.read end end end diff --git a/lib/rucaptcha/configuration.rb b/lib/rucaptcha/configuration.rb index f01bf19..0ce026a 100644 --- a/lib/rucaptcha/configuration.rb +++ b/lib/rucaptcha/configuration.rb @@ -1,5 +1,5 @@ module RuCaptcha class Configuration - attr_accessor :width, :height, :font_size, :len + attr_accessor :width, :height, :font_size, :len, :implode end end diff --git a/lib/rucaptcha/controller_helpers.rb b/lib/rucaptcha/controller_helpers.rb index 489ee79..4314833 100644 --- a/lib/rucaptcha/controller_helpers.rb +++ b/lib/rucaptcha/controller_helpers.rb @@ -7,12 +7,19 @@ module RuCaptcha end def generate_rucaptcha - session[:_rucaptcha] = SecureRandom.hex(RuCaptcha.config.len / 2) - return Captcha.create(session[:_rucaptcha]) + session[:_rucaptcha] = random_rucaptcha_chars + return RuCaptcha::Captcha.create(session[:_rucaptcha]) + end + + def random_rucaptcha_chars + chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase + chars.gsub!(/[0ol1]/i, (rand(9) + 1).to_s) + chars end def verify_rucaptcha?(resource = nil) - right = params[:_rucaptcha].strip == session[:_rucaptcha] + params[:_rucaptcha] ||= '' + right = params[:_rucaptcha].downcase.strip == session[:_rucaptcha] if resource && resource.respond_to?(:errors) resource.errors.add(:base, t('rucaptcha.invalid')) unless right end diff --git a/lib/rucaptcha/version.rb b/lib/rucaptcha/version.rb index 7981c66..6826f02 100644 --- a/lib/rucaptcha/version.rb +++ b/lib/rucaptcha/version.rb @@ -1,4 +1,4 @@ module RuCaptcha - VERSION = '0.1.1' + VERSION = '0.1.2' end diff --git a/rucaptcha.gemspec b/rucaptcha.gemspec index 8f3bb3c..b69a818 100644 --- a/rucaptcha.gemspec +++ b/rucaptcha.gemspec @@ -11,9 +11,8 @@ Gem::Specification.new do |s| s.files = Dir.glob("lib/**/*") + Dir.glob("app/**/*") + Dir.glob("config/**/*") + %w(README.md CHANGELOG.md) s.homepage = 'https://github.com/huacnlee/rucaptcha' s.require_paths = ['lib'] - s.summary = 'Captcha gem for Rails Application. It base on mini_magick to run ImageMagick command to draw Captcha image.' + s.summary = 'This is a Captcha gem for Rails Application. It run ImageMagick command to draw Captcha image.' - s.add_dependency 'mini_magick', '>= 4.0.0' s.add_dependency 'posix-spawn', '>= 0.3.0' s.add_development_dependency 'rake'