<?php
use Laminas\Captcha\Image;
$dir = __DIR__;
$captcha = new Image([
'wordLen' => 3,
'imgDir' => $dir,
'font' => __DIR__ . '/arial.ttf',
'width' => 300,
'height' => 80,
]);
$id = $captcha->generate();
$file = $captcha->getImgDir() .
$id . $captcha->getSuffix();
$img = file_get_contents($file);
$img = base64_encode($img);
echo '<img src="data:image/png;base64,' . $img . '">';
// two possible user inputs.
// id = generated session id
// input = user input
$wrongInput = [
'id' => 1,
'input' => 'asdf',
];
$rightInput = [
'id' => $captcha->getId(),
'input' => $captcha->getWord(),
];
// validate user input
dump($captcha->isValid($wrongInput));
dump($captcha->isValid($rightInput));