35 lines
960 B
PHP
35 lines
960 B
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\lib\PCaptcha;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
class CaptchaController
|
|
{
|
|
/**
|
|
* Generates and outputs a captcha image.
|
|
*/
|
|
public function get(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
|
{
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
$captcha = new PCaptcha();
|
|
$captcha->width = 200;
|
|
$captcha->hight = 50;
|
|
$captcha->leng = 6;
|
|
|
|
try {
|
|
$imageData = $captcha->get_captcha();
|
|
} catch (\Throwable $e) {
|
|
$response->getBody()->write("Captcha error: " . $e->getMessage());
|
|
return $response->withStatus(500)->withHeader('Content-Type', 'text/plain');
|
|
}
|
|
|
|
$response->getBody()->write($imageData);
|
|
return $response->withHeader('Content-Type', 'image/png');
|
|
}
|
|
}
|