Skip to content

Instantly share code, notes, and snippets.

@idlewan
Created May 1, 2014 12:38
Show Gist options
  • Save idlewan/d1e8d0af59e93a78274d to your computer and use it in GitHub Desktop.
Save idlewan/d1e8d0af59e93a78274d to your computer and use it in GitHub Desktop.
Captcha in nimrod
import cairo, math
# adapted from https://github.com/benkasminbullock/image-png-cairo/blob/master/examples/captcha.pl
# live example: http://www.lemoda.net/png/png-cairo-captcha/index.html
const xsize = 200
const ysize = 50
const gap = 10
proc create_captcha(text: string) =
var surface = image_surface_create(FORMAT_ARGB32, xsize, ysize)
var cr = create(surface)
# background
cr.set_source_rgb(0, 0, 0)
cr.rectangle(0, 0, xsize, ysize)
cr.fill()
# captcha text
cr.set_source_rgb(1, 1, 1)
cr.set_font_size(ysize - gap)
cr.move_to(gap, ysize - gap)
cr.show_text(text)
cr.fill()
# translucent circles
for i in 0 .. 50:
cr.set_source_rgba(random(1.0), random(1.0), random(1.0), 0.4)
cr.arc(float(random(xsize)), float(random(ysize)),
random(25.0), 0, 2 * PI)
cr.fill()
cr.destroy()
discard write_to_png(surface, "test.png")
when isMainModule:
var text = "abcdef?"
create_captcha(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment