Skip to content

Commit

Permalink
work on plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
traxanos committed Sep 4, 2010
1 parent a78f77a commit ea36f8a
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 4 deletions.
35 changes: 35 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@ easyCaptcha

Captcha for Rails

Configuration
=============
You can write this in "config/initializers/easy_captcha.rb", if you want to customize the default configurtion

EasyCaptcha.setup do |config|
# Font
# config.font_size = 24
# config.font_fill_color = '#222266'
# config.font_stroke_color = '#000000'
# config.font_stroke = 0
# config.font_family = File.expand_path('../../resources/afont.ttf', __FILE__)

# Image
#config.image_height = 40
#config.image_width = 140
#config.image_background_color = "#AAAAAA"

# Wave
# config.wave = true
# config.wave_length = (60..100)
# config.wave_amplitude = (3..5)

# Sketch
# config.sketch = true
# config.sketch_radius = 3
# config.sketch_sigma = 4

# Implode
# config.implode = 0.1

# Gaussian Blur
# config.blur = true
# config.blur_radius = 1
# config.blur_sigma = 2
end

Example
=======
Expand Down
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ require 'rake'
begin
require 'jeweler'
Jeweler::Tasks.new do |gemspec|
gemspec.name = "easycaptcha"
gemspec.name = "easy_captcha"
gemspec.summary = "Captcha for Rails"
gemspec.description = "A Captcha Implementation for Rails by RMagick"
gemspec.email = "[email protected]"
gemspec.homepage = "http://github.com/traxanos/easycaptcha"
gemspec.authors = ["Marco Scholl"]
gemspec.add_development_dependency "rspec", ">= 1.2.9"
gemspec.add_runtime_dependency "rails", ">= 3.0.0"
gemspec.add_runtime_dependency "rmagick"
end
Jeweler::GemcutterTasks.new
rescue LoadError
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions init.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
require 'rails'
require 'easycaptcha'
require 'easy_captcha'
54 changes: 54 additions & 0 deletions lib/easy_captcha.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module EasyCaptcha
# Font
mattr_accessor :font_size, :font_fill_color, :font_family, :font_stroke, :font_stroke_color
@@font_size = 24
@@font_fill_color = '#222266'
@@font_family = File.expand_path('../../../resources/captcha.ttf', __FILE__)
@@font_stroke = '#000000'
@@font_stroke_color = 0
# Image
mattr_accessor :image_width, :image_height, :image_background_color
@@image_width = 140
@@image_height = 40
@@image_background_color = '#AAAAAA'
# Sketch
mattr_accessor :sketch, :sketch_radius, :sketch_sigma
@@sketch = true
@@sketch_radius = 3
@@sketch_sigma = 4
# Wave
mattr_accessor :wave, :wave_length, :wave_amplitude
@@wave = true
@@wave_length = (60..100)
@@wave_amplitude = (3..5)
# Implode
mattr_accessor :implode
@@implode = 0.1
# Gaussian Blur
mattr_accessor :blur, :blur_radius, :blur_sigma
@@blur = true
@@blur_radius = 1
@@blur_sigma = 2

class << self
# setup
def setup
yield self
end

def sketch?
sketch
end

def wave?
wave
end

def blur?
blur
end
end
end

require 'easy_captcha/captcha'

54 changes: 54 additions & 0 deletions lib/easy_captcha/captcha.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module EasyCaptcha
class Captcha
attr_reader :code, :image
def initialize(code)
@code = code
generate_captcha
end

def inspect
"<EasyCaptcha::Captcha @code=#{code}>"
end

private

def generate_captcha
canvas = Magick::Image.new(EasyCaptcha.image_width, EasyCaptcha.image_height) do |variable|
self.background_color = EasyCaptcha.image_background_color unless EasyCaptcha.image_background_color.nil?
end

# Render the text in the image
canvas.annotate(Magick::Draw.new, 0,0,0,0, code) {
self.gravity = Magick::CenterGravity
self.font_family = EasyCaptcha.font_family
self.font_weight = Magick::LighterWeight
self.fill = EasyCaptcha.font_fill_color
if EasyCaptcha.font_stroke.to_i > 0
self.stroke = EasyCaptcha.font_stroke_color
self.stroke_width = EasyCaptcha.font_stroke
end
self.pointsize = EasyCaptcha.font_size
}

# Blur
canvas = canvas.blur_image(EasyCaptcha.blur_radius, EasyCaptcha.blur_sigma) if EasyCaptcha.blur?

# Wave
w = EasyCaptcha.wave_length
a = EasyCaptcha.wave_amplitude
canvas = canvas.wave(rand(a.last - a.first) + a.first, rand(w.last - w.first) + w.first) if EasyCaptcha.wave?

# Sketch
canvas = canvas.sketch(EasyCaptcha.sketch_radius, EasyCaptcha.sketch_sigma, rand(180)) if EasyCaptcha.sketch?

# Implode
canvas = canvas.implode(EasyCaptcha.implode.to_f) if EasyCaptcha.implode.is_a? Float

# Crop image because to big after waveing
canvas = canvas.crop(Magick::CenterGravity, EasyCaptcha.image_width, EasyCaptcha.image_height)

@image = canvas.to_blob { self.format = 'PNG' }
canvas.destroy!
end
end
end
20 changes: 20 additions & 0 deletions lib/easy_captcha/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module EasyCaptcha
class Config
@@image

@@config = {
:image => {
:width => 150,
:height => 30
:color => '#EEEEEE',
},
:font => {
:color => '#E20074',
:size => 20,
:ttf => File.expand_path('../../../resources/captcha.ttf', __FILE__)
}
}

cattr_accessor :image_
end
end
Empty file removed lib/easycaptcha.rb
Empty file.
2 changes: 1 addition & 1 deletion test/easycaptcha_test.rb → test/easy_captcha_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class EasycaptchaTest < ActiveSupport::TestCase
class EasyCaptchaTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
Expand Down

0 comments on commit ea36f8a

Please sign in to comment.