More complex Image render: compact text, strong lines, +/-5 rotate...

[DEPRECATION] config.width, config.height removed, use config.font_size.
Fix the render position in difference font sizes.
Fix input field type, and disable autocorrect, autocapitalize, and limit maxlength with char length.
Version 0.3.0
This commit is contained in:
Jason Lee 2015-11-05 18:20:41 +08:00
parent a8f3b15d8a
commit bcdac25b17
9 changed files with 69 additions and 35 deletions

View File

@ -1,3 +1,11 @@
0.3.0
-----
- More complex Image render: compact text, strong lines, +/-5 rotate...
- [DEPRECATION] config.width, config.height removed, use config.font_size.
- Fix the render position in difference font sizes.
- Fix input field type, and disable autocorrect, autocapitalize, and limit maxlength with char length;
0.2.5
-----

View File

@ -55,10 +55,8 @@ Create `config/initializers/rucaptcha.rb`
RuCaptcha.configure do
# Number of chars, default: 4
self.len = 4
# Image width, default: 180
self.width = 180
# Image height, default: 48
self.height = 48
# Image font size, default: 48
self.font_size = 48
# Cache generated images in file store, this is config files limit, default: 100
# set 0 to disable file cache.
self.cache_limit = 100

View File

@ -15,8 +15,7 @@ module RuCaptcha
return @config if defined?(@config)
@config = Configuration.new
@config.len = 4
@config.width = 150
@config.height = 48
@config.font_size = 48
@config.implode = 0.4
@config.cache_limit = 100
@config
@ -25,6 +24,14 @@ module RuCaptcha
def configure(&block)
config.instance_exec(&block)
if config.width != nil
ActiveSupport::Deprecation.warn("RuCaptcha config.width will remove in 0.4.0")
end
if config.height != nil
ActiveSupport::Deprecation.warn("RuCaptcha config.height will remove in 0.4.0")
end
# enable cache if cache_limit less than 1
if config.cache_limit >= 1
RuCaptcha::Captcha.send(:include, RuCaptcha::Cache)

View File

@ -4,9 +4,7 @@ module RuCaptcha
class Captcha
class << self
def rand_color
rgb = [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)]
"rgba(#{rgb.join(',')},1)"
[rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)]
end
def random_chars
@ -15,31 +13,51 @@ module RuCaptcha
chars
end
def rand_line_top(text_top, font_size)
text_top + rand(font_size - text_top * 2)
end
# Create Captcha image by code
def create(code)
size = "#{RuCaptcha.config.width}x#{RuCaptcha.config.height}"
font_size = (RuCaptcha.config.height * 0.9).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
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
full_height = full_height
half_height = full_height / 2
text_top = 10
text_left = 0 - (font_size * 0.28).to_i
stroke_width = (font_size * 0.08).to_i + 1
text_width = (full_width / chars.size) + text_left
label = "=#{' ' * (chars.size - 1)}="
text_opts = []
line_opts = []
chars.each_with_index do |char, i|
text_opts << %(-fill '#{rand_color}' -draw 'text #{(text_left + text_width) * i + text_left},#{text_top} "#{char}"')
rgb = rand_color
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_y = rand_line_top(text_top, font_size)
line_opts << %(-draw 'stroke #{line_color} line #{rand(10)},#{left_y} #{half_width + rand(half_width / 2)},#{right_y}')
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 \
convert -size #{size} \
-strokewidth #{stroke_width} \
#{line_opts.join(' ')} \
-pointsize #{font_size} -weight 700 \
#{text_opts.join(' ')} \
-wave #{rand(2) + 3}x#{rand(2) + 1} \
-rotate #{rand(10) - 5} \
-gravity NorthWest -sketch 1x10+#{rand(2)} \
-fill white \
-implode #{RuCaptcha.config.implode} label:- png:-
CODE

View File

@ -1,9 +1,9 @@
module RuCaptcha
class Configuration
# Image width, default 150
attr_accessor :width
# Image height, default 48
attr_accessor :height
# TODO: remove height, width in 0.3.0
attr_accessor :height, :width
# Image font size, default 48
attr_accessor :font_size
# Number of chars, default 4
attr_accessor :len
# implode, default 0.4

View File

@ -1,3 +1,3 @@
module RuCaptcha
VERSION = '0.2.5'
VERSION = '0.3.0'
end

View File

@ -2,7 +2,11 @@ module RuCaptcha
module ViewHelpers
def rucaptcha_input_tag(opts = {})
opts[:name] = '_rucaptcha'
opts[:type] = 'email'
opts[:type] = 'text'
opts[:autocorrect] = 'off'
opts[:autocapitalize] = 'off'
opts[:pattern] = '[0-9a-z]*'
opts[:maxlength] = RuCaptcha.config.len
opts[:autocomplete] = 'off'
tag(:input, opts)
end

View File

@ -4,8 +4,7 @@ describe RuCaptcha do
describe 'normal' do
it 'should read right config with spec_helper set' do
expect(RuCaptcha.config.len).to eq(2)
expect(RuCaptcha.config.width).to eq(123)
expect(RuCaptcha.config.height).to eq(33)
expect(RuCaptcha.config.font_size).to eq(48)
expect(RuCaptcha.config.implode).to eq(0.111)
end
end

View File

@ -21,7 +21,7 @@ end
RuCaptcha.configure do
self.len = 2
self.width = 123
self.height = 33
self.font_size = 48
self.implode = 0.111
end