Permalink
Browse files

Add simple_captcha usefull configuration, remove image_style and dist…

…ortion from get params
  • Loading branch information...
1 parent f268432 commit 17ffa77fd645ad617a6f0641f0756a42f323189f @galetahub galetahub committed Jun 8, 2010
View
@@ -31,24 +31,20 @@ backward compatibility with previous versions of Rails.
After installation, follow these simple steps to setup the plugin. The setup will depend
on the version of rails your application is using.
-===STEP 1
rails generate simple_captcha
-===STEP 2
rake db:migrate
-===STEP 3
+==Usage
+
+===Controller Based
Add the following line in the file "app/controllers/application.rb"
ApplicationController < ActionController::Base
include SimpleCaptcha::ControllerHelpers
end
-==Usage
-
-===Controller Based
-
In the view file within the form tags add this code
<%= show_simple_captcha %>
@@ -75,9 +71,9 @@ and in the model class add this code
====FormBuilder helper
- <%= form_for @page do |form| -%>
+ <%= form_for @user do |form| -%>
...
- <%= form.simple_captcha :label => "Enter numbers..", :image_style => "simply_red" %>
+ <%= form.simple_captcha :label => "Enter numbers.." %>
...
<% end -%>
@@ -100,6 +96,10 @@ SimpleCaptcha detects if your use Formtastic and appends "SimpleCaptcha::CustomF
===View Options
* *label* - provides the custom text b/w the image and the text field, the default is "type the code from the image"
+
+* *object* - the name of the object of the model class, to implement the model based captcha.
+
+===Global options
* *image_style* - provides the specific image style for the captcha image.
There are eight different styles available with the plugin as...
@@ -111,18 +111,48 @@ There are eight different styles available with the plugin as...
6) all_black
7) distorted_black
8) almost_invisible
-
Default style is 'simply_blue'.
You can also specify 'random' to select the random image style.
-
+
* *distortion* - handles the complexity of the image. The :distortion can be set to 'low', 'medium' or 'high'. Default is 'low'.
+
+Create "rails_root/config/initializers/simple_captcha.rb"
+
+ SimpleCaptcha.setup do |sc|
+ # default: 100x28
+ sc.image_size = '120x40'
-* *object* - the name of the object of the model class, to implement the model based captcha.
+ # default: 5
+ sc.length = 6
+
+ # default: simply_blue
+ # possible values:
+ # 'embosed_silver',
+ # 'simply_red',
+ # 'simply_green',
+ # 'simply_blue',
+ # 'distorted_black',
+ # 'all_black',
+ # 'charcoal_grey',
+ # 'almost_invisible'
+ # 'random'
+ sc.image_style = 'simply_green'
+
+ # default: low
+ # possible values: 'low', 'medium', 'high', 'random'
+ sc.distortion = 'medium'
+ end
-===Global options
+You can add your own style:
- SimpleCaptcha.image_size = '120x40'
- SimpleCaptcha.length = 6
+ SimpleCaptcha.setup do |sc|
+ sc.image_style = 'mycaptha'
+ sc.add_image_style('mycaptha', [
+ "-background '#F4F7F8'",
+ "-fill '#86818B'",
+ "-border 1",
+ "-bordercolor '#E0E2E3'"])
+ end
===How to change the CSS for SimpleCaptcha DOM elements?
You can change the CSS of the SimpleCaptcha DOM elements as per your need in this file.
@@ -131,11 +161,9 @@ You can change the CSS of the SimpleCaptcha DOM elements as per your need in thi
===View's Examples
====Controller Based Example
- <%= show_simple_captcha(:label => "human authentication") %>
-
- <%= show_simple_captcha(:label => "human authentication", :image_style => 'embosed_silver') %>
+ <%= show_simple_captcha %>
- <%= show_simple_captcha(:label => "human authentication", :image_style => 'simply_red', :distortion => 'medium') %>
+ <%= show_simple_captcha(:label => "human authentication") %>
====Model Based Example
@@ -1,15 +1,17 @@
-class SimpleCaptchaController < ActionController::Base
+class SimpleCaptchaController < ActionController::Metal
+ include ActionController::Streaming
include SimpleCaptcha::ImageHelpers
# GET /simple_captcha
def show
- send_file(
- generate_simple_captcha_image(
- :image_style => params[:image_style],
- :distortion => params[:distortion],
- :simple_captcha_key => params[:simple_captcha_key]),
- :type => 'image/jpeg',
- :disposition => 'inline',
- :filename => 'simple_captcha.jpg')
+ unless params[:id].blank?
+ send_file(
+ generate_simple_captcha_image(params[:id]),
+ :type => 'image/jpeg',
+ :disposition => 'inline',
+ :filename => 'simple_captcha.jpg')
+ else
+ self.response_body = [404, {"Content-Type" => "text/html"}, ["Not Found"]]
+ end
end
end
View
@@ -1,3 +1,3 @@
Rails.application.routes.draw do |map|
- match '/simple_captcha', :to => 'simple_captcha#show', :as => :simple_captcha
+ match '/simple_captcha/:id', :to => 'simple_captcha#show', :as => :simple_captcha
end
View
@@ -24,4 +24,28 @@ module SimpleCaptcha
mattr_accessor :length
@@length = 5
+
+ # 'embosed_silver',
+ # 'simply_red',
+ # 'simply_green',
+ # 'simply_blue',
+ # 'distorted_black',
+ # 'all_black',
+ # 'charcoal_grey',
+ # 'almost_invisible'
+ # 'random'
+ mattr_accessor :image_style
+ @@image_style = 'simply_blue'
+
+ # 'low', 'medium', 'high', 'random'
+ mattr_accessor :distortion
+ @@distortion = 'low'
+
+ def self.add_image_style(name, params = [])
+ SimpleCaptcha::ImageHelpers.image_styles.update(name.to_s => params)
+ end
+
+ def self.setup
+ yield self
+ end
end
@@ -11,7 +11,7 @@ module ClassMethods
# Example:
# <% form_for :post, :url => posts_path do |form| %>
# ...
- # <%= form.simple_captcha :label => "Enter numbers..", :image_style => "simply_red" %>
+ # <%= form.simple_captcha :label => "Enter numbers.." %>
# <% end %>
#
def simple_captcha(options = {})
@@ -26,7 +26,7 @@ def template
end
def simple_captcha_field(options={})
- text_field(:captcha, :value => '') +
+ text_field(:captcha, :value => '', :autocomplete => 'off') +
hidden_field(:captcha_key, {:value => options[:field_value]})
end
end
@@ -1,31 +1,42 @@
module SimpleCaptcha #:nodoc
module ImageHelpers #:nodoc
-
- IMAGE_STYLES = [
- 'embosed_silver',
- 'simply_red',
- 'simply_green',
- 'simply_blue',
- 'distorted_black',
- 'all_black',
- 'charcoal_grey',
- 'almost_invisible'
- ]
+
+ mattr_accessor :image_styles
+ @@image_styles = {
+ 'embosed_silver' => ['-fill darkblue', '-shade 20x60', '-background white'],
+ 'simply_red' => ['-fill darkred', '-background white'],
+ 'simply_green' => ['-fill darkgreen', '-background white'],
+ 'simply_blue' => ['-fill darkblue', '-background white'],
+ 'distorted_black' => ['-fill darkblue', '-edge 10', '-background white'],
+ 'all_black' => ['-fill darkblue', '-edge 2', '-background white'],
+ 'charcoal_grey' => ['-fill darkblue', '-charcoal 5', '-background white'],
+ 'almost_invisible' => ['-fill red', '-solarize 50', '-background white']
+ }
DISTORTIONS = ['low', 'medium', 'high']
class << self
- def image_style(key='simply_blue')
- return IMAGE_STYLES[rand(IMAGE_STYLES.length)] if key=='random'
- IMAGE_STYLES.include?(key) ? key : 'simply_blue'
+
+ def image_params(key = 'simply_blue')
+ image_keys = @@image_styles.keys
+
+ style = begin
+ if key == 'random'
+ image_keys[rand(image_keys.length)]
+ else
+ image_keys.include?(key) ? key : 'simply_blue'
+ end
+ end
+
+ @@image_styles[style]
end
def distortion(key='low')
key =
key == 'random' ?
DISTORTIONS[rand(DISTORTIONS.length)] :
DISTORTIONS.include?(key) ? key : 'low'
- case key
+ case key.to_s
when 'low' then return [0 + rand(2), 80 + rand(20)]
when 'medium' then return [2 + rand(2), 50 + rand(20)]
when 'high' then return [4 + rand(2), 30 + rand(20)]
@@ -42,51 +53,21 @@ def make_tmpname(basename, n)
end
private
-
- def set_simple_captcha_image_style(style) #:nodoc
- case style
- when 'embosed_silver' then
- ['darkblue', '-shade 20x60']
- when 'simply_red' then
- ['darkred', '']
- when 'simply_green' then
- ['darkgreen', '']
- when 'simply_blue' then
- ['darkblue', '']
- when 'distorted_black' then
- ['darkblue', '-edge 10']
- when 'all_black' then
- ['darkblue', '-edge 2']
- when 'charcoal_grey' then
- ['darkblue', '-charcoal 5']
- when 'almost_invisible' then
- ['red', '-solarize 50']
- else
- ['darkblue', '']
- end
- end
- def generate_simple_captcha_image(options={}) #:nodoc
- simple_captcha_options = {
- :simple_captcha_key => options[:simple_captcha_key],
- :distortion => SimpleCaptcha::ImageHelpers.distortion(options[:distortion]),
- :image_style => SimpleCaptcha::ImageHelpers.image_style(options[:image_style])
- }
- color, effect = set_simple_captcha_image_style(simple_captcha_options[:image_style])
- amplitude, frequency = simple_captcha_options[:distortion]
- text = SimpleCaptcha::Utils::simple_captcha_value(simple_captcha_options[:simple_captcha_key])
+ def generate_simple_captcha_image(simple_captcha_key) #:nodoc
+ amplitude, frequency = ImageHelpers.distortion(SimpleCaptcha.distortion)
+ text = Utils::simple_captcha_value(simple_captcha_key)
+
dst = Tempfile.new('simple_captcha.jpg')
dst.binmode
- params = [ "-size #{SimpleCaptcha.image_size}" ]
- params << "-background white"
- params << "-fill #{color}"
+ params = ImageHelpers.image_params(SimpleCaptcha.image_style).dup
+ params << "-size #{SimpleCaptcha.image_size}"
params << "-wave #{amplitude}x#{frequency}"
params << "-gravity 'Center'"
params << "-pointsize 22"
- params << effect
params << "-implode 0.2"
- params << "label:#{text} #{File.expand_path(dst.path)}"
+ params << "label:#{text} '#{File.expand_path(dst.path)}'"
SimpleCaptcha::Utils::run("convert", params.join(' '))
Oops, something went wrong.

0 comments on commit 17ffa77

Please sign in to comment.