Skip to content

Instantly share code, notes, and snippets.

@placek
Last active July 20, 2017 07:42
Show Gist options
  • Save placek/5938ec30c5791849d4be to your computer and use it in GitHub Desktop.
Save placek/5938ec30c5791849d4be to your computer and use it in GitHub Desktop.
simple captcha image generator in rmagick
require 'rmagick'
module Captcha
Image = Struct.new :text, :width, :height, :blur, :wave_length, :wave_amplitude do
def image
captcha_image = Magick::Image.new(width, height)
captcha_text = Magick::Draw.new
captcha_text.annotate(captcha_image, 0, 0, 0, height / 10, text) do
self.gravity = Magick::CenterGravity
self.pointsize = 60
self.font_family = 'Arial'
self.font_weight = Magick::BoldWeight
self.fill = 'black'
end
captcha_image.wave(wave_amplitude, wave_length).transpose.wave(wave_amplitude, wave_length).transpose.radial_blur(blur)
end
def write
file_name = ((0..5).map { Random.new.rand(0..9) } << '.png').join
image.write(file_name)
file_name
end
end
end
Captcha::Image.new('23 + 32 = ', 250, 100, 3, 100, 10).write
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment