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:
parent
a8f3b15d8a
commit
bcdac25b17
|
@ -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
|
0.2.5
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
|
@ -55,10 +55,8 @@ Create `config/initializers/rucaptcha.rb`
|
||||||
RuCaptcha.configure do
|
RuCaptcha.configure do
|
||||||
# Number of chars, default: 4
|
# Number of chars, default: 4
|
||||||
self.len = 4
|
self.len = 4
|
||||||
# Image width, default: 180
|
# Image font size, default: 48
|
||||||
self.width = 180
|
self.font_size = 48
|
||||||
# Image height, default: 48
|
|
||||||
self.height = 48
|
|
||||||
# Cache generated images in file store, this is config files limit, default: 100
|
# Cache generated images in file store, this is config files limit, default: 100
|
||||||
# set 0 to disable file cache.
|
# set 0 to disable file cache.
|
||||||
self.cache_limit = 100
|
self.cache_limit = 100
|
||||||
|
|
|
@ -15,8 +15,7 @@ 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 = 150
|
@config.font_size = 48
|
||||||
@config.height = 48
|
|
||||||
@config.implode = 0.4
|
@config.implode = 0.4
|
||||||
@config.cache_limit = 100
|
@config.cache_limit = 100
|
||||||
@config
|
@config
|
||||||
|
@ -25,6 +24,14 @@ module RuCaptcha
|
||||||
def configure(&block)
|
def configure(&block)
|
||||||
config.instance_exec(&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
|
# enable cache if cache_limit less than 1
|
||||||
if config.cache_limit >= 1
|
if config.cache_limit >= 1
|
||||||
RuCaptcha::Captcha.send(:include, RuCaptcha::Cache)
|
RuCaptcha::Captcha.send(:include, RuCaptcha::Cache)
|
||||||
|
|
|
@ -4,9 +4,7 @@ module RuCaptcha
|
||||||
class Captcha
|
class Captcha
|
||||||
class << self
|
class << self
|
||||||
def rand_color
|
def rand_color
|
||||||
rgb = [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)]
|
[rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)]
|
||||||
|
|
||||||
"rgba(#{rgb.join(',')},1)"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def random_chars
|
def random_chars
|
||||||
|
@ -15,31 +13,51 @@ module RuCaptcha
|
||||||
chars
|
chars
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def rand_line_top(text_top, font_size)
|
||||||
|
text_top + rand(font_size - text_top * 2)
|
||||||
|
end
|
||||||
|
|
||||||
# Create Captcha image by code
|
# Create Captcha image by code
|
||||||
def create(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('')
|
chars = code.split('')
|
||||||
text_opts = []
|
all_left = 20
|
||||||
text_top = (RuCaptcha.config.height - font_size) / 2
|
font_size = RuCaptcha.config.font_size
|
||||||
text_padding = 5
|
full_height = font_size
|
||||||
text_width = (RuCaptcha.config.width / chars.size) - text_padding * 2
|
full_width = (font_size * chars.size)
|
||||||
text_left = 5
|
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|
|
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
|
end
|
||||||
|
|
||||||
command = <<-CODE
|
command = <<-CODE
|
||||||
convert -size #{size} #{text_opts.join(' ')} \
|
convert -size #{size} \
|
||||||
-draw 'stroke #{line_color} line #{rand(10)},#{rand(20)} #{half_width + rand(half_width)},#{rand(half_height)}' \
|
-strokewidth #{stroke_width} \
|
||||||
-draw 'stroke #{line_color} line #{rand(10)},#{rand(25)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
|
#{line_opts.join(' ')} \
|
||||||
-draw 'stroke #{line_color} line #{rand(10)},#{rand(30)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
|
-pointsize #{font_size} -weight 700 \
|
||||||
-wave #{rand(2) + 2}x#{rand(2) + 1} \
|
#{text_opts.join(' ')} \
|
||||||
-gravity NorthWest -sketch 1x10+#{rand(1)} -pointsize #{font_size} -weight 700 \
|
-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:-
|
-implode #{RuCaptcha.config.implode} label:- png:-
|
||||||
CODE
|
CODE
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
module RuCaptcha
|
module RuCaptcha
|
||||||
class Configuration
|
class Configuration
|
||||||
# Image width, default 150
|
# TODO: remove height, width in 0.3.0
|
||||||
attr_accessor :width
|
attr_accessor :height, :width
|
||||||
# Image height, default 48
|
# Image font size, default 48
|
||||||
attr_accessor :height
|
attr_accessor :font_size
|
||||||
# Number of chars, default 4
|
# Number of chars, default 4
|
||||||
attr_accessor :len
|
attr_accessor :len
|
||||||
# implode, default 0.4
|
# implode, default 0.4
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
module RuCaptcha
|
module RuCaptcha
|
||||||
VERSION = '0.2.5'
|
VERSION = '0.3.0'
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,11 @@ module RuCaptcha
|
||||||
module ViewHelpers
|
module ViewHelpers
|
||||||
def rucaptcha_input_tag(opts = {})
|
def rucaptcha_input_tag(opts = {})
|
||||||
opts[:name] = '_rucaptcha'
|
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'
|
opts[:autocomplete] = 'off'
|
||||||
tag(:input, opts)
|
tag(:input, opts)
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,8 +4,7 @@ describe RuCaptcha do
|
||||||
describe 'normal' do
|
describe 'normal' do
|
||||||
it 'should read right config with spec_helper set' do
|
it 'should read right config with spec_helper set' do
|
||||||
expect(RuCaptcha.config.len).to eq(2)
|
expect(RuCaptcha.config.len).to eq(2)
|
||||||
expect(RuCaptcha.config.width).to eq(123)
|
expect(RuCaptcha.config.font_size).to eq(48)
|
||||||
expect(RuCaptcha.config.height).to eq(33)
|
|
||||||
expect(RuCaptcha.config.implode).to eq(0.111)
|
expect(RuCaptcha.config.implode).to eq(0.111)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,7 +21,7 @@ end
|
||||||
|
|
||||||
RuCaptcha.configure do
|
RuCaptcha.configure do
|
||||||
self.len = 2
|
self.len = 2
|
||||||
self.width = 123
|
|
||||||
self.height = 33
|
self.height = 33
|
||||||
|
self.font_size = 48
|
||||||
self.implode = 0.111
|
self.implode = 0.111
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue