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.
This commit is contained in:
Jason Lee 2015-10-26 17:40:59 +08:00
parent 7c672bbc92
commit 3be8125c40
8 changed files with 64 additions and 30 deletions

View File

@ -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). - Include default validation I18n messages (en, zh-CN, zh-TW).

View File

@ -1,7 +1,7 @@
# RuCaptcha # 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. So it's NO performance issue, and memory leak issue.
@ -10,7 +10,6 @@ Idea by: https://ruby-china.org/topics/20558#reply4
### Requirements ### Requirements
- ImageMagick - ImageMagick
- [mini_magick](https://github.com/minimagick/minimagick)
### Usage ### Usage
@ -30,8 +29,6 @@ RuCaptcha.configure do
self.width = 180 self.width = 180
# Image height, default: 48 # Image height, default: 48
self.height = 48 self.height = 48
# Font size, default: 38
self.font_size = 38
end end
``` ```

View File

@ -3,6 +3,7 @@ require_relative 'rucaptcha/version'
require_relative 'rucaptcha/configuration' require_relative 'rucaptcha/configuration'
require_relative 'rucaptcha/controller_helpers' require_relative 'rucaptcha/controller_helpers'
require_relative 'rucaptcha/view_helpers' require_relative 'rucaptcha/view_helpers'
require_relative 'rucaptcha/captcha'
require_relative 'rucaptcha/engine' require_relative 'rucaptcha/engine'
module RuCaptcha module RuCaptcha
@ -11,9 +12,9 @@ module RuCaptcha
return @config if defined?(@config) return @config if defined?(@config)
@config = Configuration.new @config = Configuration.new
@config.len = 4 @config.len = 4
@config.width = 180 @config.width = 150
@config.height = 48 @config.height = 48
@config.font_size = 38 @config.implode = 0.4
@config @config
end end

View File

@ -2,23 +2,44 @@ require 'posix-spawn'
module RuCaptcha module RuCaptcha
class Captcha class Captcha
class << self def self.rand_color
def create(code) r = rand(129).to_s(8).to_i
color = "rgba(#{rand(100)},#{rand(100)},#{rand(100)}, 1)" rgb = [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)]
size = "#{RuCaptcha.config.width}x#{RuCaptcha.config.height}"
command = <<-CODE "rgba(#{rgb.join(',')},1)"
convert -size #{size} -fill "#{color}" -background white \ end
-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)}' \ def self.create(code)
-wave #{2 + rand(2)}x#{80 + rand(20)} \ size = "#{RuCaptcha.config.width}x#{RuCaptcha.config.height}"
-gravity Center -sketch 2x19+#{rand(6)} -pointsize #{RuCaptcha.config.font_size} -implode 0.3 label:#{code.upcase} png:- font_size = (RuCaptcha.config.height * 0.8).to_i
CODE half_width = RuCaptcha.config.width / 2
command.strip! half_height = RuCaptcha.config.height / 2
# puts command line_color = rand_color
pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command)
Process.waitpid(pid) chars = code.split('')
stdout.read 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 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 end
end end

View File

@ -1,5 +1,5 @@
module RuCaptcha module RuCaptcha
class Configuration class Configuration
attr_accessor :width, :height, :font_size, :len attr_accessor :width, :height, :font_size, :len, :implode
end end
end end

View File

@ -7,12 +7,19 @@ module RuCaptcha
end end
def generate_rucaptcha def generate_rucaptcha
session[:_rucaptcha] = SecureRandom.hex(RuCaptcha.config.len / 2) session[:_rucaptcha] = random_rucaptcha_chars
return Captcha.create(session[:_rucaptcha]) 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 end
def verify_rucaptcha?(resource = nil) 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) if resource && resource.respond_to?(:errors)
resource.errors.add(:base, t('rucaptcha.invalid')) unless right resource.errors.add(:base, t('rucaptcha.invalid')) unless right
end end

View File

@ -1,4 +1,4 @@
module RuCaptcha module RuCaptcha
VERSION = '0.1.1' VERSION = '0.1.2'
end end

View File

@ -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.files = Dir.glob("lib/**/*") + Dir.glob("app/**/*") + Dir.glob("config/**/*") + %w(README.md CHANGELOG.md)
s.homepage = 'https://github.com/huacnlee/rucaptcha' s.homepage = 'https://github.com/huacnlee/rucaptcha'
s.require_paths = ['lib'] 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_dependency 'posix-spawn', '>= 0.3.0'
s.add_development_dependency 'rake' s.add_development_dependency 'rake'