From bcdac25b17b7bb58feaf62bfc0fda1d4af44790d Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 5 Nov 2015 18:20:41 +0800 Subject: [PATCH] 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 --- CHANGELOG.md | 8 +++++ README.md | 6 ++-- lib/rucaptcha.rb | 11 +++++-- lib/rucaptcha/captcha.rb | 58 ++++++++++++++++++++++------------ lib/rucaptcha/configuration.rb | 8 ++--- lib/rucaptcha/version.rb | 2 +- lib/rucaptcha/view_helpers.rb | 6 +++- spec/configure_spec.rb | 3 +- spec/spec_helper.rb | 2 +- 9 files changed, 69 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c53b24..3702dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ----- diff --git a/README.md b/README.md index b8beebe..415ebda 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/rucaptcha.rb b/lib/rucaptcha.rb index ccb0472..f5531c6 100644 --- a/lib/rucaptcha.rb +++ b/lib/rucaptcha.rb @@ -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) diff --git a/lib/rucaptcha/captcha.rb b/lib/rucaptcha/captcha.rb index e291029..1b22a6a 100644 --- a/lib/rucaptcha/captcha.rb +++ b/lib/rucaptcha/captcha.rb @@ -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 diff --git a/lib/rucaptcha/configuration.rb b/lib/rucaptcha/configuration.rb index 570ce97..6fea8d3 100644 --- a/lib/rucaptcha/configuration.rb +++ b/lib/rucaptcha/configuration.rb @@ -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 diff --git a/lib/rucaptcha/version.rb b/lib/rucaptcha/version.rb index 1de28df..880e0f8 100644 --- a/lib/rucaptcha/version.rb +++ b/lib/rucaptcha/version.rb @@ -1,3 +1,3 @@ module RuCaptcha - VERSION = '0.2.5' + VERSION = '0.3.0' end diff --git a/lib/rucaptcha/view_helpers.rb b/lib/rucaptcha/view_helpers.rb index 18d5568..f9fbec6 100644 --- a/lib/rucaptcha/view_helpers.rb +++ b/lib/rucaptcha/view_helpers.rb @@ -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 diff --git a/spec/configure_spec.rb b/spec/configure_spec.rb index 3063bea..a384113 100644 --- a/spec/configure_spec.rb +++ b/spec/configure_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 86cb03b..eebdc01 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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