Sha256: f0c30e59a46e28c9210fea36900fac100aa434697bb067c29c3c2da1d53e0c60

Contents?: true

Size: 1.09 KB

Versions: 9

Compression:

Stored size: 1.09 KB

Contents

# coding: utf-8

require 'RMagick'

module RailsCaptcha
  class Image

    include Magick
    attr_reader :code, :data

    def initialize(o)
      generate_code o

      canvas = Magick::Image.new(o[:dimensions][:width], o[:dimensions][:height]) {
        self.background_color = o[:colors][:background]
      }

      text = Magick::Draw.new
      text.font = File.expand_path o[:ttf]
      text.pointsize = o[:letters][:points]

      cur = 0
      @code.each { |c|
        text.annotate(canvas, 0, 0, cur, o[:letters][:baseline], c) {
          self.fill = o[:colors][:font]
        }
        cur += o[:letters][:width]
      }

      w = o[:wave][:wavelength]
      canvas = canvas.wave(o[:wave][:amplitude], rand(w.last - w.first) + w.first)
      canvas = canvas.implode(o[:implode])

      @code = @code.to_s
      @data = canvas.to_blob { self.format = "JPG" }
      canvas.destroy!
    end

    private

    def generate_code(o)
      chars = ('a'..'z').to_a - o[:letters][:ignore]
      @code = []
      1.upto(o[:letters][:count]) do
        @code << chars[rand(chars.length)]
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rails_captcha-0.0.10 lib/rails_captcha/captcha/image.rb
rails_captcha-0.0.8 lib/rails_captcha/captcha/image.rb
rails_captcha-0.0.7 lib/rails_captcha/captcha/image.rb
rails_captcha-0.0.6 lib/rails_captcha/captcha/image.rb
rails_captcha-0.0.5 lib/rails_captcha/captcha/image.rb
rails_captcha-0.0.4 lib/rails_captcha/captcha/image.rb
rails_captcha-0.0.3 lib/rails_captcha/captcha/image.rb
rails_captcha-0.0.2 lib/rails_captcha/captcha/image.rb
rails_captcha-0.0.1 lib/rails_captcha/captcha/image.rb