easy_captcha 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -12,51 +12,62 @@ after bundle execute
12
12
  You can configure easy_captcha in "config/initializers/easy_captcha.rb", if you want to customize the default configuration
13
13
 
14
14
  EasyCaptcha.setup do |config|
15
-
16
15
  # Cache
17
- # config.cache = true
16
+ # config.cache = true
18
17
  # Cache temp dir from Rails.root
19
18
  # config.cache_temp_dir = Rails.root + 'tmp' + 'captchas'
20
19
  # Cache size
21
- # config.cache_size = 500
20
+ # config.cache_size = 500
22
21
  # Cache expire
23
- # config.cache_expire = 1.days
22
+ # config.cache_expire = 1.days
24
23
 
25
24
  # Chars
26
- # config.chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
25
+ # config.chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
26
+
27
27
  # Length
28
- # config.length = 6
29
- # Font
30
- # config.font_size = 24
31
- # config.font_fill_color = '#333333'
32
- # config.font_stroke_color = '#000000'
33
- # config.font_stroke = 0
34
- # config.font_family = File.expand_path('../../resources/afont.ttf', __FILE__)
35
-
28
+ # config.length = 6
29
+
36
30
  # Image
37
- #config.image_height = 40
38
- #config.image_width = 140
39
- #config.image_background_color = "#FFFFFF"
40
-
41
- # Wave
42
- # config.wave = true
43
- # config.wave_length = (60..100)
44
- # config.wave_amplitude = (3..5)
45
-
46
- # Sketch
47
- # config.sketch = true
48
- # config.sketch_radius = 3
49
- # config.sketch_sigma = 1
50
-
51
- # Implode
52
- # config.implode = 0.1
53
-
54
- # Blur
55
- # config.blur = true
56
- # config.blur_radius = 1
57
- # config.blur_sigma = 2
31
+ # config.image_height = 40
32
+ # config.image_width = 140
33
+
34
+ # configure generator
35
+ # config.generator :default do |generator|
36
+
37
+ # Font
38
+ # generator.font_size = 24
39
+ # generator.font_fill_color = '#333333'
40
+ # generator.font_stroke_color = '#000000'
41
+ # generator.font_stroke = 0
42
+ # generator.font_family = File.expand_path('../../resources/afont.ttf', __FILE__)
43
+
44
+ # generator.image_background_color = "#FFFFFF"
45
+
46
+ # Wave
47
+ # generator.wave = true
48
+ # generator.wave_length = (60..100)
49
+ # generator.wave_amplitude = (3..5)
50
+
51
+ # Sketch
52
+ # generator.sketch = true
53
+ # generator.sketch_radius = 3
54
+ # generator.sketch_sigma = 1
55
+
56
+ # Implode
57
+ # generator.implode = 0.1
58
+
59
+ # Blur
60
+ # generator.blur = true
61
+ # generator.blur_radius = 1
62
+ # generator.blur_sigma = 2
63
+ # end
58
64
  end
59
65
 
66
+ == Requirements
67
+
68
+ * RMagick
69
+ * Rails 3 (http://github.com/rails/rails)
70
+
60
71
  == Example
61
72
 
62
73
  <% form_tag '/' do %>
@@ -72,9 +83,10 @@ You can configure easy_captcha in "config/initializers/easy_captcha.rb", if you
72
83
  You find an example app under: http://github.com/traxanos/easy_captcha_example
73
84
 
74
85
  == History
75
- * 0.1 Init
76
- * 0.2 Cache support for high frequented sites
86
+ * 0.1 init
87
+ * 0.2 cache support for high frequented sites
77
88
  * 0.3 use generators, optimizations, update licence to same of all my plugins
89
+ * 0.4 generator support
78
90
 
79
91
  == Maintainers
80
92
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
data/easy_captcha.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{easy_captcha}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marco Scholl"]
@@ -29,6 +29,9 @@ Gem::Specification.new do |s|
29
29
  "lib/easy_captcha/captcha.rb",
30
30
  "lib/easy_captcha/controller.rb",
31
31
  "lib/easy_captcha/controller_helpers.rb",
32
+ "lib/easy_captcha/generator.rb",
33
+ "lib/easy_captcha/generator/base.rb",
34
+ "lib/easy_captcha/generator/default.rb",
32
35
  "lib/easy_captcha/model_helpers.rb",
33
36
  "lib/easy_captcha/routes.rb",
34
37
  "lib/easy_captcha/view_helpers.rb",
data/lib/easy_captcha.rb CHANGED
@@ -11,6 +11,7 @@ module EasyCaptcha
11
11
  autoload :ModelHelpers, 'easy_captcha/model_helpers'
12
12
  autoload :ViewHelpers, 'easy_captcha/view_helpers'
13
13
  autoload :ControllerHelpers, 'easy_captcha/controller_helpers'
14
+ autoload :Generator, 'easy_captcha/generator'
14
15
 
15
16
  # Cache
16
17
  mattr_accessor :cache
@@ -31,39 +32,15 @@ module EasyCaptcha
31
32
  # Chars
32
33
  mattr_accessor :chars
33
34
  @@chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
35
+
34
36
  # Length
35
37
  mattr_accessor :length
36
38
  @@length = 6
37
- # Font
38
- mattr_accessor :font_size, :font_fill_color, :font_family, :font_stroke, :font_stroke_color
39
- @@font_size = 24
40
- @@font_fill_color = '#333333'
41
- @@font_family = File.expand_path('../../../resources/captcha.ttf', __FILE__)
42
- @@font_stroke = '#000000'
43
- @@font_stroke_color = 0
44
- # Image
45
- mattr_accessor :image_width, :image_height, :image_background_color
46
- @@image_width = 140
39
+
40
+ # Length
41
+ mattr_accessor :image_width, :image_height
42
+ @@image_width = 140
47
43
  @@image_height = 40
48
- @@image_background_color = '#FFFFFF'
49
- # Sketch
50
- mattr_accessor :sketch, :sketch_radius, :sketch_sigma
51
- @@sketch = true
52
- @@sketch_radius = 3
53
- @@sketch_sigma = 1
54
- # Wave
55
- mattr_accessor :wave, :wave_length, :wave_amplitude
56
- @@wave = true
57
- @@wave_length = (60..100)
58
- @@wave_amplitude = (3..5)
59
- # Implode
60
- mattr_accessor :implode
61
- @@implode = 0.05
62
- # Gaussian Blur
63
- mattr_accessor :blur, :blur_radius, :blur_sigma
64
- @@blur = true
65
- @@blur_radius = 1
66
- @@blur_sigma = 2
67
44
 
68
45
  class << self
69
46
  # to configure easy_captcha
@@ -72,28 +49,57 @@ module EasyCaptcha
72
49
  yield self
73
50
  end
74
51
 
75
- def sketch? #:nodoc:
76
- sketch
52
+ def cache? #:nodoc:
53
+ cache
77
54
  end
78
55
 
79
- def wave? #:nodoc:
80
- wave
81
- end
56
+ # select generator and configure this
57
+ def generator(generator = nil, &block)
58
+ if generator.nil?
59
+ @generator
60
+ else
61
+ generator = generator.to_s if generator.is_a? Symbol
82
62
 
83
- def blur? #:nodoc:
84
- blur
63
+ if generator.is_a? String
64
+ generator.gsub!(/^[a-z]|\s+[a-z]/) { |a| a.upcase }
65
+ generator = "EasyCaptcha::Generator::#{generator}".constantize
66
+ end
67
+
68
+ @generator = generator.new &block
69
+ end
85
70
  end
86
71
 
87
- def cache? #:nodoc:
88
- cache
72
+ # depracated
73
+ def method_missing name, *args
74
+ depracations = [
75
+ :font_size, :font_fill_color, :font_family, :font_stroke, :font_stroke_color,
76
+ :image_background_color, :sketch, :sketch_radius, :sketch_sigma, :wave,
77
+ :wave_length, :wave_amplitude, :implode, :blur, :blur_radius, :blur_sigma
78
+ ]
79
+
80
+ if depracations.include? name[0..-2].to_sym or depracations.include? name.to_sym
81
+ ActiveSupport::Deprecation.warn "EasyCaptcha.#{name} is deprecated."
82
+ if name[-1] == '='
83
+ self.generator.send(name, args.first)
84
+ else
85
+ self.generator.send(name)
86
+ end
87
+ else
88
+ super
89
+ end
89
90
  end
90
91
 
92
+
91
93
  # called by rails after initialize
92
94
  def init
93
95
  require 'easy_captcha/routes'
94
96
  ActiveRecord::Base.send :include, ModelHelpers
95
97
  ActionController::Base.send :include, ControllerHelpers
96
98
  ActionView::Base.send :include, ViewHelpers
99
+
100
+ # set default generator
101
+ generator :default
102
+
97
103
  end
98
104
  end
99
105
  end
@@ -19,45 +19,7 @@ module EasyCaptcha
19
19
  private
20
20
 
21
21
  def generate_captcha(file = nil) #:nodoc:
22
- canvas = Magick::Image.new(EasyCaptcha.image_width, EasyCaptcha.image_height) do |variable|
23
- self.background_color = EasyCaptcha.image_background_color unless EasyCaptcha.image_background_color.nil?
24
- end
25
-
26
- # Render the text in the image
27
- canvas.annotate(Magick::Draw.new, 0, 0, 0, 0, code) {
28
- self.gravity = Magick::CenterGravity
29
- self.font_family = EasyCaptcha.font_family
30
- self.font_weight = Magick::LighterWeight
31
- self.fill = EasyCaptcha.font_fill_color
32
- if EasyCaptcha.font_stroke.to_i > 0
33
- self.stroke = EasyCaptcha.font_stroke_color
34
- self.stroke_width = EasyCaptcha.font_stroke
35
- end
36
- self.pointsize = EasyCaptcha.font_size
37
- }
38
-
39
- # Blur
40
- canvas = canvas.blur_image(EasyCaptcha.blur_radius, EasyCaptcha.blur_sigma) if EasyCaptcha.blur?
41
-
42
- # Wave
43
- w = EasyCaptcha.wave_length
44
- a = EasyCaptcha.wave_amplitude
45
- canvas = canvas.wave(rand(a.last - a.first) + a.first, rand(w.last - w.first) + w.first) if EasyCaptcha.wave?
46
-
47
- # Sketch
48
- canvas = canvas.sketch(EasyCaptcha.sketch_radius, EasyCaptcha.sketch_sigma, rand(180)) if EasyCaptcha.sketch?
49
-
50
- # Implode
51
- canvas = canvas.implode(EasyCaptcha.implode.to_f) if EasyCaptcha.implode.is_a? Float
52
-
53
- # Crop image because to big after waveing
54
- canvas = canvas.crop(Magick::CenterGravity, EasyCaptcha.image_width, EasyCaptcha.image_height)
55
-
56
- unless file.nil?
57
- canvas.write(file) { self.format = 'PNG' }
58
- end
59
- @image = canvas.to_blob { self.format = 'PNG' }
60
- canvas.destroy!
22
+ @image = EasyCaptcha.generator.generate(@code, file)
61
23
  end
62
24
  end
63
25
  end
@@ -0,0 +1,8 @@
1
+ module EasyCaptcha
2
+ # module for generators
3
+ module Generator
4
+ autoload :Base, 'easy_captcha/generator/base'
5
+ autoload :Default, 'easy_captcha/generator/default'
6
+
7
+ end
8
+ end
@@ -0,0 +1,19 @@
1
+ module EasyCaptcha
2
+ module Generator
3
+
4
+ # base class for generators
5
+ class Base
6
+
7
+ # generator for captcha images
8
+ def initialize(&block)
9
+ defaults
10
+ yield self if block_given?
11
+ end
12
+
13
+ # default values for generator
14
+ def defaults
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,104 @@
1
+ module EasyCaptcha
2
+ module Generator
3
+
4
+ # default generator
5
+ class Default < Base
6
+
7
+ # set default values
8
+ def defaults
9
+ @font_size = 24
10
+ @font_fill_color = '#333333'
11
+ @font_family = File.expand_path('../../../../resources/captcha.ttf', __FILE__)
12
+ @font_stroke = '#000000'
13
+ @font_stroke_color = 0
14
+ @image_background_color = '#FFFFFF'
15
+ @sketch = true
16
+ @sketch_radius = 3
17
+ @sketch_sigma = 1
18
+ @wave = true
19
+ @wave_length = (60..100)
20
+ @wave_amplitude = (3..5)
21
+ @implode = 0.05
22
+ @blur = true
23
+ @blur_radius = 1
24
+ @blur_sigma = 2
25
+ end
26
+
27
+ # Font
28
+ attr_accessor :font_size, :font_fill_color, :font_family, :font_stroke, :font_stroke_color
29
+
30
+ # Background
31
+ attr_accessor :image_background_color
32
+
33
+ # Sketch
34
+ attr_accessor :sketch, :sketch_radius, :sketch_sigma
35
+
36
+ # Wave
37
+ attr_accessor :wave, :wave_length, :wave_amplitude
38
+
39
+ # Implode
40
+ attr_accessor :implode
41
+
42
+ # Gaussian Blur
43
+ attr_accessor :blur, :blur_radius, :blur_sigma
44
+
45
+ def sketch? #:nodoc:
46
+ @sketch
47
+ end
48
+
49
+ def wave? #:nodoc:
50
+ @wave
51
+ end
52
+
53
+ def blur? #:nodoc:
54
+ @blur
55
+ end
56
+
57
+ # generate image
58
+ def generate(code, file = nil)
59
+ config = self
60
+ canvas = Magick::Image.new(EasyCaptcha.image_width, EasyCaptcha.image_height) do |variable|
61
+ self.background_color = config.image_background_color unless config.image_background_color.nil?
62
+ end
63
+
64
+ # Render the text in the image
65
+ canvas.annotate(Magick::Draw.new, 0, 0, 0, 0, code) {
66
+ self.gravity = Magick::CenterGravity
67
+ self.font_family = config.font_family
68
+ self.font_weight = Magick::LighterWeight
69
+ self.fill = config.font_fill_color
70
+ if config.font_stroke.to_i > 0
71
+ self.stroke = config.font_stroke_color
72
+ self.stroke_width = config.font_stroke
73
+ end
74
+ self.pointsize = config.font_size
75
+ }
76
+
77
+ # Blur
78
+ canvas = canvas.blur_image(config.blur_radius, config.blur_sigma) if config.blur?
79
+
80
+ # Wave
81
+ w = config.wave_length
82
+ a = config.wave_amplitude
83
+ canvas = canvas.wave(rand(a.last - a.first) + a.first, rand(w.last - w.first) + w.first) if config.wave?
84
+
85
+ # Sketch
86
+ canvas = canvas.sketch(config.sketch_radius, config.sketch_sigma, rand(180)) if config.sketch?
87
+
88
+ # Implode
89
+ canvas = canvas.implode(config.implode.to_f) if config.implode.is_a? Float
90
+
91
+ # Crop image because to big after waveing
92
+ canvas = canvas.crop(Magick::CenterGravity, EasyCaptcha.image_width, EasyCaptcha.image_height)
93
+
94
+ unless file.nil?
95
+ canvas.write(file) { self.format = 'PNG' }
96
+ end
97
+ image = canvas.to_blob { self.format = 'PNG' }
98
+ canvas.destroy!
99
+ image
100
+ end
101
+
102
+ end
103
+ end
104
+ end
@@ -1,44 +1,51 @@
1
1
  EasyCaptcha.setup do |config|
2
2
  # Cache
3
- # config.cache = true
3
+ # config.cache = true
4
4
  # Cache temp dir from Rails.root
5
5
  # config.cache_temp_dir = Rails.root + 'tmp' + 'captchas'
6
6
  # Cache size
7
- # config.cache_size = 500
7
+ # config.cache_size = 500
8
8
  # Cache expire
9
- # config.cache_expire = 1.days
9
+ # config.cache_expire = 1.days
10
10
 
11
11
  # Chars
12
- # config.chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
12
+ # config.chars = %w(2 3 4 5 6 7 9 A C D E F G H J K L M N P Q R S T U X Y Z)
13
+
13
14
  # Length
14
- # config.length = 6
15
- # Font
16
- # config.font_size = 24
17
- # config.font_fill_color = '#333333'
18
- # config.font_stroke_color = '#000000'
19
- # config.font_stroke = 0
20
- # config.font_family = File.expand_path('../../resources/afont.ttf', __FILE__)
15
+ # config.length = 6
21
16
 
22
17
  # Image
23
- #config.image_height = 40
24
- #config.image_width = 140
25
- #config.image_background_color = "#FFFFFF"
26
-
27
- # Wave
28
- # config.wave = true
29
- # config.wave_length = (60..100)
30
- # config.wave_amplitude = (3..5)
31
-
32
- # Sketch
33
- # config.sketch = true
34
- # config.sketch_radius = 3
35
- # config.sketch_sigma = 1
36
-
37
- # Implode
38
- # config.implode = 0.1
39
-
40
- # Blur
41
- # config.blur = true
42
- # config.blur_radius = 1
43
- # config.blur_sigma = 2
18
+ # config.image_height = 40
19
+ # config.image_width = 140
20
+
21
+ # configure generator
22
+ # config.generator :default do |generator|
23
+
24
+ # Font
25
+ # generator.font_size = 24
26
+ # generator.font_fill_color = '#333333'
27
+ # generator.font_stroke_color = '#000000'
28
+ # generator.font_stroke = 0
29
+ # generator.font_family = File.expand_path('../../resources/afont.ttf', __FILE__)
30
+
31
+ # generator.image_background_color = "#FFFFFF"
32
+
33
+ # Wave
34
+ # generator.wave = true
35
+ # generator.wave_length = (60..100)
36
+ # generator.wave_amplitude = (3..5)
37
+
38
+ # Sketch
39
+ # generator.sketch = true
40
+ # generator.sketch_radius = 3
41
+ # generator.sketch_sigma = 1
42
+
43
+ # Implode
44
+ # generator.implode = 0.1
45
+
46
+ # Blur
47
+ # generator.blur = true
48
+ # generator.blur_radius = 1
49
+ # generator.blur_sigma = 2
50
+ # end
44
51
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Marco Scholl
@@ -138,6 +138,9 @@ files:
138
138
  - lib/easy_captcha/captcha.rb
139
139
  - lib/easy_captcha/controller.rb
140
140
  - lib/easy_captcha/controller_helpers.rb
141
+ - lib/easy_captcha/generator.rb
142
+ - lib/easy_captcha/generator/base.rb
143
+ - lib/easy_captcha/generator/default.rb
141
144
  - lib/easy_captcha/model_helpers.rb
142
145
  - lib/easy_captcha/routes.rb
143
146
  - lib/easy_captcha/view_helpers.rb
@@ -161,7 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
164
  requirements:
162
165
  - - ">="
163
166
  - !ruby/object:Gem::Version
164
- hash: 3005660918227227729
167
+ hash: -88900623567721278
165
168
  segments:
166
169
  - 0
167
170
  version: "0"