winton-captcha 1.0.1 → 1.0.2
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/lib/captcha.rb +46 -0
- data/lib/captcha/actions.rb +26 -0
- data/resources/captcha.ttf +0 -0
- metadata +5 -4
data/lib/captcha.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'RMagick'
|
2
|
+
|
3
|
+
Dir[File.expand_path('*/*.rb', File.dirname(__FILE__))].each do |f|
|
4
|
+
require [ File.dirname(f), File.basename(f, '.rb') ].join('/')
|
5
|
+
end
|
6
|
+
|
7
|
+
class Captcha
|
8
|
+
|
9
|
+
include Magick
|
10
|
+
attr_reader :code, :code_image
|
11
|
+
|
12
|
+
JIGGLE = 15
|
13
|
+
WOBBLE = 25
|
14
|
+
|
15
|
+
def initialize(len)
|
16
|
+
chars = ('a'..'z').to_a - ['a','e','i','o','u','l','j']
|
17
|
+
code_array = []
|
18
|
+
1.upto(len) { code_array << chars[rand(chars.length)] }
|
19
|
+
granite = Magick::ImageList.new('granite:')
|
20
|
+
canvas = Magick::ImageList.new
|
21
|
+
canvas.new_image(32*len, 50, Magick::TextureFill.new(granite))
|
22
|
+
text = Magick::Draw.new
|
23
|
+
text.font = File.expand_path('vendor/plugins/captcha/resources/captcha.ttf')
|
24
|
+
text.pointsize = 40
|
25
|
+
cur = 10
|
26
|
+
|
27
|
+
code_array.each { |c|
|
28
|
+
rot = rand(10) > 5 ? rand(WOBBLE) : -rand(WOBBLE)
|
29
|
+
weight = rand(10) > 5 ? NormalWeight : BoldWeight
|
30
|
+
text.annotate(canvas,0,0,cur,30+rand(JIGGLE), c) {
|
31
|
+
self.rotation = rot
|
32
|
+
self.font_weight = weight
|
33
|
+
self.fill = '#98999B'
|
34
|
+
}
|
35
|
+
cur += 30
|
36
|
+
}
|
37
|
+
|
38
|
+
@code = code_array.to_s
|
39
|
+
@code_image = canvas.to_blob {
|
40
|
+
self.format = "JPG"
|
41
|
+
}
|
42
|
+
|
43
|
+
GC.start
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module CaptchaActions
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def acts_as_captcha
|
9
|
+
include CaptchaActions::InstanceMethods
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
def index
|
15
|
+
new unless session[:captcha]
|
16
|
+
send_file "#{RAILS_ROOT}/public/images/captchas/#{session[:captcha]}.jpg", :type => 'image/jpeg', :disposition => 'inline'
|
17
|
+
end
|
18
|
+
|
19
|
+
def new
|
20
|
+
files = Dir["#{RAILS_ROOT}/public/images/captchas/*.jpg"]
|
21
|
+
session[:captcha] = File.basename(files[rand(files.length)], '.jpg')
|
22
|
+
index
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: winton-captcha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Winton Welsh
|
@@ -23,10 +23,11 @@ extra_rdoc_files: []
|
|
23
23
|
|
24
24
|
files:
|
25
25
|
- init.rb
|
26
|
-
- lib
|
27
|
-
- lib
|
26
|
+
- lib/captcha.rb
|
27
|
+
- lib/captcha
|
28
|
+
- lib/captcha/actions.rb
|
28
29
|
- README.markdown
|
29
|
-
- resources
|
30
|
+
- resources/captcha.ttf
|
30
31
|
has_rdoc: false
|
31
32
|
homepage: http://github.com/winton/captcha
|
32
33
|
post_install_message:
|