?
<?php
namespace App\Http\Controllers\Traits;
trait CaptchaGeneratorTrait
{
public function generateImageCaptcha()
{
$chars = 'abcdefghjkmnpqrstuvwxyz23456789';
$captchaText = substr(str_shuffle($chars), 0, 6);
session(['captcha_answer' => $captchaText]);
$width = 120;
$height = 40;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);
$lineColor = imagecolorallocate($image, 200, 200, 200);
imagefilledrectangle($image, 0, 0, $width, $height, $bgColor);
for ($i = 0; $i < 5; $i++) {
imageline($image, 0, rand() % $height, $width, rand() % $height, $lineColor);
}
imagestring($image, 5, 30, 12, $text, $textColor);
ob_start();
imagepng($image);
$imageData = ob_get_clean();
imagedestroy($image);
return 'data:image/png;base64,' . base64_encode($imageData);
}
public function generateMathCaptcha()
{
$num1 = rand(1, 20);
$num2 = rand(1, 20);
$result = $num1 + $num2;
session(['captcha_answer' => $result]);
return "$num1 + $num2";
}
}