Last active
July 20, 2017 07:42
-
-
Save placek/5938ec30c5791849d4be to your computer and use it in GitHub Desktop.
simple captcha image generator in rmagick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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