forked from phatworx/easy_captcha
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
167 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
require 'rails' | ||
require 'easycaptcha' | ||
require 'easy_captcha' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters