This change introduces ImageLines, an interface for adding randomized lines to captcha images for enhanced security. The interface has been implemented in the Lines class. The goal is to randomize lines on captcha images to prevent bot reads.

This commit is contained in:
Leonid Nikitin 2023-06-28 17:24:41 +06:00
parent f2ecdfcf97
commit c1cf5a1ae9
Signed by: kor-elf
GPG Key ID: 7DE8F80C5CEC2C0D
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
namespace App\Captcha\Contracts;
interface ImageLines
{
public function processing(Image $image, array $colors, int $lines = 3): Image;
}

View File

@ -0,0 +1,36 @@
<?php declare(strict_types=1);
namespace App\Captcha\Images;
use App\Captcha\Contracts\ImageLines;
use Illuminate\Support\Arr;
use App\Captcha\Contracts\Image as ImageContract;
class Lines implements ImageLines
{
public function processing(ImageContract $image, array $colors, $lines = 3): ImageContract
{
$imageWidth = $image->getWidth();
$imageHeight = $image->getHeight();
for ($i = 0; $i <= $lines; $i++) {
$image->addLine(
x1: mt_rand(0, $imageWidth) + $i * mt_rand(0, $imageHeight),
y1: mt_rand(0, $imageHeight),
x2: mt_rand(0, $imageWidth),
y2: mt_rand(0, $imageHeight),
hexColor: $this->lineColors($colors)
);
}
return $image;
}
private function lineColors(array $colors): string
{
if (!empty($colors)) {
return Arr::random($colors);
}
return '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
}
}