captcha3 1.0.1 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- 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 +20 -0
- data/lib/captcha3/image_generator.rb +57 -0
- data/lib/captcha3/railtie.rb +8 -0
- data/lib/captcha3/tasks.rb +23 -0
- data/lib/captcha3/validate_captcha.rb +18 -0
- data/lib/captcha3/version.rb +3 -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,20 @@
|
|
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
|
+
|
12
|
+
if defined?(CAPTCHA_SALT)
|
13
|
+
salt = CAPTCHA_SALT
|
14
|
+
else
|
15
|
+
Rails.logger.warn("No Salt has given, Please add CAPACHA_SALT = 'something realy random' to environment.rb or application.rb")
|
16
|
+
end
|
17
|
+
|
18
|
+
Digest::SHA256.hexdigest("#{salt}#{str}")
|
19
|
+
end
|
20
|
+
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,23 @@
|
|
1
|
+
require "captcha3/image_generator"
|
2
|
+
namespace :captcha3 do
|
3
|
+
desc "generate code images"
|
4
|
+
task :generate => :environment do
|
5
|
+
FileUtils.mkdir_p('public/system/captcha3')
|
6
|
+
Dir.chdir('public/system/captcha3') do
|
7
|
+
image_count = (ENV["count"] || 200).to_i
|
8
|
+
image_params = resolve_params
|
9
|
+
|
10
|
+
image_count.times.each do
|
11
|
+
ImageGenerator.generate_captcha_image(image_params)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def resolve_params
|
17
|
+
params = {}
|
18
|
+
['image_width', 'image_height', 'captcha_length', 'file_type'].each do |origin|
|
19
|
+
params[origin.downcase.to_sym] = ENV[origin] if ENV[origin]
|
20
|
+
end
|
21
|
+
params
|
22
|
+
end
|
23
|
+
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
|
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: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 5
|
10
|
+
version: 1.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- victordong
|
@@ -31,7 +31,7 @@ dependencies:
|
|
31
31
|
version: "0"
|
32
32
|
type: :runtime
|
33
33
|
version_requirements: *id001
|
34
|
-
description:
|
34
|
+
description: dongsx
|
35
35
|
email:
|
36
36
|
- dongshuxiang888@126.com
|
37
37
|
executables: []
|
@@ -41,7 +41,18 @@ extensions: []
|
|
41
41
|
extra_rdoc_files: []
|
42
42
|
|
43
43
|
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- Rakefile
|
47
|
+
- captcha3.gemspec
|
44
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
|
|
@@ -71,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
82
|
requirements: []
|
72
83
|
|
73
84
|
rubyforge_project: captcha3
|
74
|
-
rubygems_version: 1.8.
|
85
|
+
rubygems_version: 1.8.17
|
75
86
|
signing_key:
|
76
87
|
specification_version: 3
|
77
88
|
summary: !binary |
|