module

CrImage::Util::Captcha

CAPTCHA generation utility

Generates CAPTCHA images with distorted text, noise, and interference patterns to prevent automated OCR while remaining human-readable.

Example:

captcha = CrImage::Util::Captcha.generate(
  text: "ABC123",
  font_path: "fonts/Roboto-Bold.ttf",
  width: 300,
  height: 100
)
CrImage::PNG.write("captcha.png", captcha)

Class methods

generate(text : String, font_path : String, options : Options = Options.new, font_index : Int32 = 0) : RGBA

Generate a CAPTCHA image with the given text

Parameters:

  • text: The text to display in the CAPTCHA
  • font_path: Path to TrueType font file (.ttf, .otf, .ttc)
  • options: CAPTCHA generation options (optional)
  • font_index: Font index for TrueType Collection (.ttc) files (default: 0)

Returns: RGBA image containing the CAPTCHA

Source
random_text(length : Int32 = 6, charset : String = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789") : String

Generate random CAPTCHA text

Parameters:

  • length: Number of characters (default: 6)
  • charset: Character set to use (default: alphanumeric without confusing chars)

Returns: Random text string

Source

Nested types