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).

View File

@ -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
```

View File

@ -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

View File

@ -2,16 +2,38 @@ require 'posix-spawn'
module RuCaptcha
class Captcha
class << self
def create(code)
color = "rgba(#{rand(100)},#{rand(100)},#{rand(100)}, 1)"
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} -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:-
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
@ -20,5 +42,4 @@ module RuCaptcha
stdout.read
end
end
end
end

View File

@ -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

View File

@ -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

View File

@ -1,4 +1,4 @@
module RuCaptcha
VERSION = '0.1.1'
VERSION = '0.1.2'
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.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'