Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
First image generation poc
Browse files Browse the repository at this point in the history
  • Loading branch information
reu committed Mar 11, 2011
1 parent dccaa9e commit a01761a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
.bundle
pkg/*
vendor/*
tmp/*
2 changes: 2 additions & 0 deletions lib/traptcha.rb
@@ -1,2 +1,4 @@
require 'traptcha/image'

module Traptcha
end
34 changes: 34 additions & 0 deletions lib/traptcha/image.rb
@@ -0,0 +1,34 @@
module Traptcha
class Image
def initialize(text)
@text = text
@canvas = Magick::Image.new(150, 100)
@canvas.format = "PNG"
end

def save(path)
generate
@canvas.write(File.join(path, @text + ".png"))
end

protected

def generate
image = ::Magick::Draw.new

image.font_family = "Thonburi"
image.font_weight = ::Magick::BoldWeight

position = 0

@text.chars.each do |letter|
image.annotate(@canvas, 0, 0, position, 90, letter) do
self.fill = "black"
self.pointsize = (20..50).to_a.choice
end

position += (20..25).to_a.choice
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
@@ -0,0 +1 @@
require 'traptcha'
22 changes: 22 additions & 0 deletions spec/traptcha/image_spec.rb
@@ -0,0 +1,22 @@
require "spec_helper"
require "rmagick"
require "fileutils"

describe Traptcha::Image do
describe "save" do
subject { Traptcha::Image.new("test") }
let(:path) { File.expand_path(File.dirname(__FILE__) + "../../../tmp/image_spec") }

before do
FileUtils.mkdir_p path
end

it "saves a image file on an specific path" do
expect { subject.save(path) }.to change { Dir["#{path}/*"].size }
end

after do
FileUtils.rm_rf path
end
end
end

0 comments on commit a01761a

Please sign in to comment.