captcha3 1.1.5 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/captcha3.gemspec +24 -0
- data/lib/captcha3/captcha_helper.rb +19 -0
- data/lib/captcha3/captcha_util.rb +18 -0
- data/lib/captcha3/image_generator.rb +57 -0
- data/lib/captcha3/railtie.rb +8 -0
- data/lib/captcha3/tasks.rb +24 -0
- data/lib/captcha3/validate_captcha.rb +18 -0
- data/lib/captcha3/version.rb +3 -0
- data/lib/captcha3.rb +14 -0
- metadata +16 -5
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/captcha3.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "captcha3/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "captcha3"
|
7
|
+
s.version = Captcha3::VERSION
|
8
|
+
s.authors = ["victordong"]
|
9
|
+
s.email = ["dongshuxiang888@126.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{批量生成图片}
|
12
|
+
s.description = %q{dongsx}
|
13
|
+
|
14
|
+
s.rubyforge_project = "captcha3"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
s.add_runtime_dependency "rmagick"
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CaptchaHelper
|
2
|
+
def captcha_image(options = {})
|
3
|
+
@captcha_image ||= CaptchaUtil.random_image
|
4
|
+
image_tag("/system/captcha3/" + @captcha_image, options)
|
5
|
+
end
|
6
|
+
|
7
|
+
def captcha_input_text(label, options = {})
|
8
|
+
content_tag('label', label, :for => 'captcha') + text_field_tag(:captcha, nil, options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def captcha_hidden_text
|
12
|
+
@captcha_image ||= CaptchaUtil.random_image
|
13
|
+
hidden_field_tag(:captcha_validation, @captcha_image.gsub(/\..+$/, ''))
|
14
|
+
end
|
15
|
+
|
16
|
+
def captcha_block(label='请输入验证码')
|
17
|
+
content_tag("div", captcha_hidden_text + captcha_input_text(label) + captcha_image, {:class => 'captcha'})
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
module CaptchaUtil
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def random_image
|
6
|
+
@@captcha_files = Dir.glob("#{Rails.root}/public/system/captcha3/*.*").map{ |f| File.basename(f)}
|
7
|
+
@@captcha_files[rand(@@captcha_files.size)]
|
8
|
+
end
|
9
|
+
|
10
|
+
def encrypt_string(str)
|
11
|
+
if defined?(CAPTCHA_SALT)
|
12
|
+
salt = CAPTCHA_SALT
|
13
|
+
else
|
14
|
+
raise "---You must add CAPTCHA_SALT in config/initializers/captcha3.rb---"
|
15
|
+
end
|
16
|
+
Digest::SHA256.hexdigest("#{salt}#{str}")
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'RMagick'
|
2
|
+
require 'captcha3/captcha_util'
|
3
|
+
module ImageGenerator
|
4
|
+
extend self
|
5
|
+
BITS = ("A".."Z").to_a + (0..9).to_a
|
6
|
+
BIT_LENGTH = BITS.length
|
7
|
+
DEFAULT_PARAS = {
|
8
|
+
:image_width => 150,
|
9
|
+
:image_height => 50,
|
10
|
+
:captcha_length => 5,
|
11
|
+
:file_type => 'png'
|
12
|
+
}
|
13
|
+
|
14
|
+
def generate_captcha_image(params = {})
|
15
|
+
params.reverse_merge!(DEFAULT_PARAS)
|
16
|
+
file_type = %w(png gif).include?(params[:file_type]) ? ".#{params[:file_type]}" : ".png"
|
17
|
+
|
18
|
+
text_img = Magick::Image.new(params[:image_width].to_i, params[:image_height].to_i)
|
19
|
+
black_img = Magick::Image.new(params[:image_width].to_i, params[:image_height].to_i) do
|
20
|
+
self.background_color = 'black'
|
21
|
+
end
|
22
|
+
|
23
|
+
random_string = params[:captcha_length].times.map{ BITS[rand(BIT_LENGTH)]}.join
|
24
|
+
|
25
|
+
filename = CaptchaUtil.encrypt_string(random_string.downcase) + file_type
|
26
|
+
|
27
|
+
# Render the text in the image
|
28
|
+
text_img.annotate(Magick::Draw.new, 0,0,0,0, random_string) {
|
29
|
+
self.gravity = Magick::WestGravity
|
30
|
+
self.font_family = 'Courier-New'
|
31
|
+
self.font_weight = Magick::BoldWeight
|
32
|
+
self.fill = '#000000'
|
33
|
+
self.stroke = 'black'
|
34
|
+
self.stroke_width = 1
|
35
|
+
self.pointsize = 44
|
36
|
+
}
|
37
|
+
|
38
|
+
# Apply a little blur and fuzzing
|
39
|
+
text_img = text_img.gaussian_blur(1.2, 1.2)
|
40
|
+
text_img = text_img.sketch(20, 30.0, 30.0)
|
41
|
+
text_img = text_img.wave(3, 90)
|
42
|
+
|
43
|
+
# Now we need to get the white out
|
44
|
+
text_mask = text_img.negate
|
45
|
+
text_mask.matte = false
|
46
|
+
|
47
|
+
# Add cut-out our captcha from the black image with varying tranparency
|
48
|
+
black_img.composite!(text_mask, Magick::CenterGravity, Magick::CopyOpacityCompositeOp)
|
49
|
+
|
50
|
+
# Write the file to disk
|
51
|
+
puts 'Writing image file ' + filename
|
52
|
+
black_img.write(filename) # { self.depth = 8 }
|
53
|
+
|
54
|
+
# Collect rmagick
|
55
|
+
GC.start
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "captcha3/image_generator"
|
2
|
+
require "captcha3/generate_salt"
|
3
|
+
namespace :captcha3 do
|
4
|
+
desc "generate code images"
|
5
|
+
task :generate => :environment do
|
6
|
+
FileUtils.mkdir_p('public/system/captcha3')
|
7
|
+
Dir.chdir('public/system/captcha3') do
|
8
|
+
image_count = (ENV["count"] || 200).to_i
|
9
|
+
image_params = resolve_params
|
10
|
+
|
11
|
+
image_count.times.each do
|
12
|
+
ImageGenerator.generate_captcha_image(image_params)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def resolve_params
|
18
|
+
params = {}
|
19
|
+
['image_width', 'image_height', 'captcha_length', 'file_type'].each do |origin|
|
20
|
+
params[origin.downcase.to_sym] = ENV[origin] if ENV[origin]
|
21
|
+
end
|
22
|
+
params
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ValidateCaptcha
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(ClassMethods)
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def captcha_helper
|
8
|
+
helper CaptchaHelper
|
9
|
+
include ValidateCaptcha::InstanceMethods
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
def captcha_validated?
|
15
|
+
CaptchaUtil.encrypt_string(params[:captcha].to_s.downcase) == params[:captcha_validation]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/captcha3.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "captcha3/version"
|
2
|
+
require "captcha3/validate_captcha"
|
3
|
+
require "captcha3/captcha_helper"
|
4
|
+
require "captcha3/image_generator"
|
5
|
+
module Captcha3
|
6
|
+
require 'captcha3/railtie' if defined?(Rails)
|
7
|
+
ActionController::Base.send(:include, ValidateCaptcha)
|
8
|
+
if defined?(Rails)
|
9
|
+
environment = File.open("#{Rails.root}/config/environment.rb", "a")
|
10
|
+
salt = SecureRandom.base64(32).tr("+/=", "xyz")
|
11
|
+
environment.puts("CAPTCHA_SALT = #{salt}")
|
12
|
+
environment.close
|
13
|
+
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: captcha3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 1.1.
|
9
|
+
- 6
|
10
|
+
version: 1.1.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- victordong
|
@@ -40,8 +40,19 @@ extensions: []
|
|
40
40
|
|
41
41
|
extra_rdoc_files: []
|
42
42
|
|
43
|
-
files:
|
44
|
-
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- Rakefile
|
47
|
+
- captcha3.gemspec
|
48
|
+
- lib/captcha3.rb
|
49
|
+
- lib/captcha3/captcha_helper.rb
|
50
|
+
- lib/captcha3/captcha_util.rb
|
51
|
+
- lib/captcha3/image_generator.rb
|
52
|
+
- lib/captcha3/railtie.rb
|
53
|
+
- lib/captcha3/tasks.rb
|
54
|
+
- lib/captcha3/validate_captcha.rb
|
55
|
+
- lib/captcha3/version.rb
|
45
56
|
homepage: ""
|
46
57
|
licenses: []
|
47
58
|
|