Sha256: 61962fe837e9e517ccaa82cbe6708fd9231ede291636e3ad77763acf9ac004a6

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

module Captcher
  class TextImage
    attr_accessor :text, :config

    def initialize(text, config)
      @text = text
      @config = config
    end

    # rubocop:disable Metrics/AbcSize
    # rubocop:disable Metrics/MethodLength
    # rubocop:disable Lint/Void
    def generate
      MiniMagick::Tool::Convert.new do |i|
        i.font      random_font
        i.size      image_size
        i.pointsize config[:font_size]
        i.fill      config[:font_color]
        i.gravity   "center"
        i.canvas    config[:background]
        i.draw      "text 0,0 '#{text}'"
        i.noise + "Gaussian"
        i << "#{config[:format]}:-"
      end
    end
    # rubocop:enable Lint/Void
    # rubocop:enable Metrics/MethodLength
    # rubocop:enable Metrics/AbcSize

    private

    def image_size
      "#{image_width}x#{image_height}"
    end

    def image_height
      config[:font_size] + 10
    end

    def image_width
      config[:font_size] * config[:count] + 10
    end

    def random_font
      config[:fonts].sample
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
captcher-0.3.1 lib/captcher/text_image.rb
captcher-0.3.0 lib/captcher/text_image.rb
captcher-0.2.1 lib/captcher/text_image.rb
captcher-0.2.0 lib/captcher/text_image.rb
captcher-0.1.1 lib/captcher/text_image.rb
captcher-0.1.0 lib/captcher/text_image.rb