Permalink
Browse files

Add charset setting, more smart character spacing and positioning, ad…

…d rake test_images
  • Loading branch information...
1 parent 6f3fb17 commit c0b175035d1f94ea552b45d1fb104c4b34952885 @glebtv committed Mar 19, 2013
Showing with 157 additions and 48 deletions.
  1. +18 −20 .gitignore
  2. +104 −0 Rakefile
  3. +0 −2 init.rb
  4. +18 −15 lib/simple_captcha.rb
  5. +5 −3 lib/simple_captcha/image.rb
  6. +9 −7 lib/simple_captcha/utils.rb
  7. +1 −1 lib/simple_captcha/version.rb
  8. +2 −0 simple_captcha.gemspec
View
@@ -1,21 +1,19 @@
-pkg/
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
Gemfile.lock
-.rspec
-spec/tmp
-spec/dummy/log/*.log
-spec/dummy/tmp/
-spec/dummy/config/*.yml
-spec/dummy/config/*.conf
-spec/dummy/config/logrotate-config
-spec/dummy/config/nginx-config
-spec/dummy/db/schema.rb
-spec/dummy/db/sphinx/
-spec/dummy/public/cache/
-spec/dummy/public/assets/
-spec/dummy/public/ckeditor_assets/
-spec/dummy/public/uploads/
-spec/dummy/vendor/bundle/
-spec/dummy/app/helpers/manage
-spec/dummy/config/database.yml
-spec/dummy/log/development.log
-.idea
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
+.idea
+test_images
View
104 Rakefile
@@ -1 +1,105 @@
require "bundler/gem_tasks"
+
+task :test_images do
+ require 'fileutils'
+ require 'active_support/all'
+ require File.join(File.dirname(__FILE__), "lib", "simple_captcha")
+
+ SimpleCaptcha::Utils # autoload
+
+ module SimpleCaptcha #:nodoc
+ module Utils #:nodoc
+ def self.simple_captcha_new_value(key) #:nodoc
+ self.random_str
+ end
+ end
+
+ class Test #:nodoc
+ include SimpleCaptcha::ImageHelpers
+
+ def run
+ generate_simple_captcha_image('test')
+ end
+ end
+ end
+
+ tmp_path = File.join(File.dirname(__FILE__), 'test_images')
+
+ Dir.mkdir(tmp_path, 0777) unless File.directory?(tmp_path)
+
+ tmp_file = SimpleCaptcha::Test.new.run
+ FileUtils.mv(tmp_file, File.join(tmp_path, '130x40_font30.png'))
+
+ SimpleCaptcha.setup do |sc|
+ sc.image_size = '120x60'
+ sc.length = 5
+ sc.image_color = "#31203C"
+ sc.image_style = 'distorted_black'
+ sc.distortion = 'medium'
+ end
+
+ tmp_file = SimpleCaptcha::Test.new.run
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
+
+ SimpleCaptcha.setup do |sc|
+ sc.image_size = '120x40'
+ # sc.image_color = "#31203C"
+ sc.image_style = 'distorted_black'
+ sc.distortion = 'medium'
+ end
+
+ tmp_file = SimpleCaptcha::Test.new.run
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
+
+
+ SimpleCaptcha.setup do |sc|
+ sc.pointsize = 20
+ sc.image_color = '#666666'
+ sc.image_size = '100x30'
+ end
+ tmp_file = SimpleCaptcha::Test.new.run
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
+
+ SimpleCaptcha.setup do |sc|
+ sc.pointsize = 40
+ sc.image_color = '#666666'
+ sc.image_size = '150x60'
+ end
+ tmp_file = SimpleCaptcha::Test.new.run
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
+
+
+ SimpleCaptcha.setup do |sc|
+ sc.pointsize = 16
+ sc.image_color = '#0000ff'
+ sc.image_size = '80x20'
+ end
+ tmp_file = SimpleCaptcha::Test.new.run
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
+
+ SimpleCaptcha.setup do |sc|
+ sc.pointsize = 14
+ sc.image_color = '#0000ff'
+ sc.image_size = '70x16'
+ end
+ tmp_file = SimpleCaptcha::Test.new.run
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
+
+ SimpleCaptcha.setup do |sc|
+ sc.pointsize = 16
+ sc.image_color = '#0000ff'
+ sc.image_size = '80x20'
+ end
+ tmp_file = SimpleCaptcha::Test.new.run
+ FileUtils.mv(tmp_file, File.join(tmp_path, "#{SimpleCaptcha.image_size}_font#{SimpleCaptcha.pointsize}.png"))
+
+
+ SimpleCaptcha.setup do |sc|
+ sc.pointsize = 40
+ sc.distortion = 'high'
+ sc.image_color = '#666666'
+ sc.image_size = '150x60'
+ end
+ tmp_file = SimpleCaptcha::Test.new.run
+ FileUtils.mv(tmp_file, File.join(tmp_path, "distorted.png"))
+end
View
@@ -1,2 +0,0 @@
-# encoding: utf-8
-require File.join(File.dirname(__FILE__), "lib", "simple_captcha")
View
@@ -1,37 +1,40 @@
# encoding: utf-8
module SimpleCaptcha
- autoload :Utils, 'simple_captcha/utils'
+ autoload :Utils, File.dirname(__FILE__) + '/simple_captcha/utils'
- autoload :ImageHelpers, 'simple_captcha/image'
- autoload :ViewHelper, 'simple_captcha/view'
- autoload :ControllerHelpers, 'simple_captcha/controller'
+ autoload :ImageHelpers, File.dirname(__FILE__) + '/simple_captcha/image'
+ autoload :ViewHelper, File.dirname(__FILE__) + '/simple_captcha/view'
+ autoload :ControllerHelpers, File.dirname(__FILE__) + '/simple_captcha/controller'
- autoload :FormBuilder, 'simple_captcha/hooks/form_builder'
+ autoload :FormBuilder, File.dirname(__FILE__) + '/simple_captcha/hooks/form_builder'
- if Object.const_defined?("Formtastic")
- require "simple_captcha/hooks/formtastic"
+ if Object.const_defined?('Formtastic')
+ require File.dirname(__FILE__) + '/simple_captcha/hooks/formtastic'
end
- if Object.const_defined?("SimpleForm")
- require "simple_captcha/hooks/simple_form"
+ if Object.const_defined?('SimpleForm')
+ require File.dirname(__FILE__) + '/simple_captcha/hooks/simple_form'
end
- if Object.const_defined?("Mongoid")
- autoload :SimpleCaptchaData, 'simple_captcha/storage/mongoid'
+ if Object.const_defined?('Mongoid')
+ autoload :SimpleCaptchaData, File.dirname(__FILE__) + '/simple_captcha/storage/mongoid'
else
- autoload :SimpleCaptchaData, 'simple_captcha/storage/active_record'
- autoload :ModelHelpers, 'simple_captcha/active_record'
+ autoload :SimpleCaptchaData, File.dirname(__FILE__) + '/simple_captcha/storage/active_record'
+ autoload :ModelHelpers, File.dirname(__FILE__) + '/simple_captcha/active_record'
end
- autoload :Middleware, 'simple_captcha/middleware'
+ autoload :Middleware, File.dirname(__FILE__) + '/simple_captcha/middleware'
mattr_accessor :image_size
- @@image_size = "100x28"
+ @@image_size = "130x40"
mattr_accessor :length
@@length = 5
+ mattr_accessor :charset
+ @@charset = '23456789abcdefghkmnpqrstwxyz'
+
# 'embosed_silver',
# 'simply_red',
# 'simply_green',
@@ -72,14 +72,16 @@ def generate_simple_captcha_image(simple_captcha_key) #:nodoc
params << "-size #{SimpleCaptcha.image_size} xc:transparent"
params << "-gravity \"Center\""
+ psz = SimpleCaptcha.pointsize
if params.join(' ').index('-pointsize').nil?
- params << "-pointsize #{SimpleCaptcha.pointsize}"
+ params << "-pointsize #{psz}"
end
+
dst = Tempfile.new(RUBY_VERSION < '1.9' ? 'simple_captcha.png' : ['simple_captcha', '.png'], SimpleCaptcha.tmp_path)
dst.binmode
text.split(//).each_with_index do |letter, index|
- i = -60 + (index*25) + rand(-6..6)
- params << "-draw \"translate #{i},#{rand(-6..6)} skewX #{rand(-15..15)} gravity center text 0,0 '#{letter}'\" "
+ i = -(1.5 * psz) + (index * 0.75 * psz) + rand(-3..3)
+ params << "-draw \"translate #{i},#{rand(-3..3)} skewX #{rand(-15..15)} gravity center text 0,0 '#{letter}'\" "
end
params << "-wave #{amplitude}x#{frequency}"
@@ -24,17 +24,19 @@ def self.simple_captcha_value(key) #:nodoc
SimpleCaptchaData.get_data(key).value rescue nil
end
+ def self.random_str()
+ charset = SimpleCaptcha.charset.split(//)
+ size = SimpleCaptcha.length
+
+ (0...size).map{ charset.to_a[rand(charset.size)] }.join
+ end
+
def self.simple_captcha_new_value(key) #:nodoc
begin
- # very unsafe to display same code over and over
- value = ''
- # SimpleCaptcha.length.times{value << (48 + rand(10)).chr}
-
- SimpleCaptcha.length.times{value << (65 + rand(26)).chr}
d = SimpleCaptchaData.get_data(key)
- d.value = value
+ d.value = self.random_str
d.save!
- value
+ d.value
rescue
nil
end
@@ -1,4 +1,4 @@
# encoding: utf-8
module SimpleCaptcha
- VERSION = "0.3.1".freeze
+ VERSION = "0.4.1".freeze
end
View
@@ -16,4 +16,6 @@ Gem::Specification.new do |s|
s.test_files = Dir["{test}/**/*"]
s.extra_rdoc_files = %w(README.rdoc)
s.require_paths = %w(lib)
+
+ s.add_development_dependency("activesupport", ["~> 3.2"])
end

0 comments on commit c0b1750

Please sign in to comment.