Skip to content
This repository was archived by the owner on Jan 30, 2024. It is now read-only.

fix and improvements #35

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions easy_captcha.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {spec}/*`.split("\n")
s.require_paths = ["lib"]

s.add_dependency('rails', [">= 3.0.0"])
s.add_dependency('bundler', [">= 1.1.0"])
s.add_dependency('simplecov', [">= 0.3.8"])
s.add_dependency('rspec-rails', [">= 2.8.1"])
s.add_dependency('yard', [">= 0.7.0"])
s.add_dependency('rails', [">= 4.1.0"])

s.add_development_dependency('bundler', [">= 1.1.0"])
s.add_development_dependency('simplecov', [">= 0.3.8"])
s.add_development_dependency('rspec-rails', [">= 2.8.1"])
s.add_development_dependency('yard', [">= 0.7.0"])

if defined?(PLATFORM) && PLATFORM == 'java'
s.add_runtime_dependency('rmagick4j','>= 0.3.7')
Expand Down
2 changes: 1 addition & 1 deletion lib/easy_captcha/captcha_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module EasyCaptcha
# captcha controller
class CaptchaController < ActionController::Base
before_filter :overwrite_cache_control
before_action :overwrite_cache_control
# captcha action send the generated image to browser
def captcha
if params[:format] == "wav" and EasyCaptcha.espeak?
Expand Down
30 changes: 18 additions & 12 deletions lib/easy_captcha/generator/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ def defaults
@font_size = 24
@font_fill_color = '#333333'
@font = File.expand_path('../../../../resources/captcha.ttf', __FILE__)
@font_stroke = '#000000'
@font_stroke_color = 0
@font_stroke = 0
@font_stroke_color = '#000000'
@image_background_color = '#FFFFFF'
@background_image = nil
@sketch = true
@sketch_radius = 3
@sketch_sigma = 1
Expand Down Expand Up @@ -61,13 +62,14 @@ def generate(code)
require 'rmagick' unless defined?(Magick)

config = self
canvas = Magick::Image.new(EasyCaptcha.image_width, EasyCaptcha.image_height) do |variable|
canvas = Magick::Image.new(EasyCaptcha.image_width, EasyCaptcha.image_height) do |_variable|
self.background_color = config.image_background_color unless config.image_background_color.nil?
self.background_color = 'none' if config.background_image.present?
end

# Render the text in the image
canvas.annotate(Magick::Draw.new, 0, 0, 0, 0, code) {
# canvas.annotate(Magick::Draw.new, 0, 0, 0, 0, code) {
Magick::Draw.new.annotate(canvas, 0, 0, 0, 0, code) {
self.gravity = Magick::CenterGravity
self.font = config.font
self.font_weight = Magick::LighterWeight
Expand All @@ -88,29 +90,33 @@ def generate(code)
canvas = canvas.wave(rand(a.last - a.first) + a.first, rand(w.last - w.first) + w.first) if config.wave?

# Sketch
canvas = canvas.sketch(config.sketch_radius, config.sketch_sigma, rand(180)) if config.sketch?
canvas = canvas.sketch(config.sketch_radius, config.sketch_sigma, rand(180)) if config.sketch? && canvas.respond_to?(:sketch)

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

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


# Combine images if background image is present
if config.background_image.present?
background = Magick::Image.read(config.background_image).first
background.composite!(canvas, Magick::CenterGravity, Magick::OverCompositeOp)
if config.background_image.blank?
image = canvas.to_blob { self.format = 'PNG' }
else
#Background Random Position
gravit = [Magick::NorthWestGravity, Magick::NorthGravity, Magick::NorthEastGravity, Magick::WestGravity,
Magick::CenterGravity, Magick::EastGravity, Magick::SouthWestGravity, Magick::SouthGravity,
Magick::SouthEastGravity].sample

background = Magick::Image.read(config.background_image).first
background.composite!(canvas, gravit, Magick::OverCompositeOp)
background = background.crop(gravit, EasyCaptcha.image_width, EasyCaptcha.image_height)
image = background.to_blob { self.format = 'PNG' }
else
image = canvas.to_blob { self.format = 'PNG' }
end

# ruby-1.9
image = image.force_encoding 'UTF-8' if image.respond_to? :force_encoding

canvas.destroy!
canvas.destroy! if canvas.respond_to?('destroy!')
image
end

Expand Down
2 changes: 1 addition & 1 deletion lib/easy_captcha/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module EasyCaptcha
VERSION = '0.6.5'
VERSION = '0.6.6'
end
2 changes: 1 addition & 1 deletion lib/easy_captcha/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module ViewHelpers
def captcha_tag(*args)
options = { :alt => 'captcha', :width => EasyCaptcha.image_width, :height => EasyCaptcha.image_height }
options.merge! args.extract_options!
image_tag(captcha_url(:i => Time.now.to_i), options)
image_tag(captcha_path(:i => Time.now.to_i), options)
end
end
end
8 changes: 4 additions & 4 deletions spec/espeak_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
context 'check default values' do
subject { EasyCaptcha::Espeak.new }

its(:amplitude) { (80..120).should include(subject.amplitude) }
its(:pitch) { (30..70).should include(subject.pitch) }
its(:gap) { should eq 80 }
its(:voice) { should be_nil }
it(:amplitude) { (80..120).should include(subject.amplitude) }
it(:pitch) { (30..70).should include(subject.pitch) }
it(:gap) { should eq 80 }
it(:voice) { should be_nil }
end

context 'check config: voices' do
Expand Down