Add captcha image generation functionality.

This commit is contained in:
2023-06-28 17:29:56 +06:00
parent 81635b4efa
commit a35b8db281
26 changed files with 1211 additions and 5 deletions

View File

@@ -0,0 +1,89 @@
<?php declare(strict_types=1);
namespace App\Captcha\Dto;
final readonly class Coordinators
{
public function __construct(
private int $x1,
private int $y1,
private int $x2,
private int $y2,
private int $x3,
private int $y3,
private int $x4,
private int $y4
) { }
/**
* lower left x-coordinate
* @return int
*/
public function getX1(): int
{
return $this->x1;
}
/**
* lower left y-coordinate
* @return int
*/
public function getY1(): int
{
return $this->y1;
}
/**
* lower right x-coordinate
* @return int
*/
public function getX2(): int
{
return $this->x2;
}
/**
* lower right y-coordinate
* @return int
*/
public function getY2(): int
{
return $this->y2;
}
/**
* upper right x-coordinate
* @return int
*/
public function getX3(): int
{
return $this->x3;
}
/**
* upper right y-coordinate
* @return int
*/
public function getY3(): int
{
return $this->y3;
}
/**
* upper left x-coordinate
* @return int
*/
public function getX4(): int
{
return $this->x4;
}
/**
* upper left y-coordinate
* @return int
*/
public function getY4(): int
{
return $this->y4;
}
}

36
app/Captcha/Dto/Image.php Normal file
View File

@@ -0,0 +1,36 @@
<?php declare(strict_types=1);
namespace App\Captcha\Dto;
final readonly class Image
{
public function __construct(
private string $imageBase64,
private int $width,
private int $height
) { }
/**
* @return string
*/
public function getImageBase64(): string
{
return $this->imageBase64;
}
/**
* @return int
*/
public function getWidth(): int
{
return $this->width;
}
/**
* @return int
*/
public function getHeight(): int
{
return $this->height;
}
}

View File

@@ -0,0 +1,27 @@
<?php declare(strict_types=1);
namespace App\Captcha\Dto;
final readonly class ImageBody
{
public function __construct(
private Image $image,
private array $coordinators
) { }
/**
* @return Image
*/
public function getImage(): Image
{
return $this->image;
}
/**
* @return Coordinators
*/
public function getCoordinators(): array
{
return $this->coordinators;
}
}

View File

@@ -0,0 +1,18 @@
<?php declare(strict_types=1);
namespace App\Captcha\Dto;
final readonly class ImageHead
{
public function __construct(
private Image $image
) { }
/**
* @return Image
*/
public function getImage(): Image
{
return $this->image;
}
}

View File

@@ -0,0 +1,45 @@
<?php declare(strict_types=1);
namespace App\Captcha\Dto;
final readonly class Sector
{
public function __construct(
private int $x,
private int $y,
private int $width,
private int $height,
) { }
/**
* @return int
*/
public function getX(): int
{
return $this->x;
}
/**
* @return int
*/
public function getY(): int
{
return $this->y;
}
/**
* @return int
*/
public function getWidth(): int
{
return $this->width;
}
/**
* @return int
*/
public function getHeight(): int
{
return $this->height;
}
}

View File

@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
namespace App\Captcha\Dto;
final class Sectors
{
private array $points = [];
public function add(int|float $x, int|float $y, int|float $width, int|float $height): self
{
$this->points[] = new Sector((int) $x, (int) $y, (int) $width, (int) $height);
return $this;
}
public function random(): Sector
{
$key = array_rand($this->points);
$sector = $this->points[$key];
unset($this->points[$key]);
return $sector;
}
}

View File

@@ -0,0 +1,38 @@
<?php declare(strict_types=1);
namespace App\Captcha\Dto;
use App\Captcha\Enums\SymbolType;
final readonly class Symbols
{
public function __construct(
private array $success,
private array $fakes,
private SymbolType $type
) { }
/**
* @return array
*/
public function getSuccess(): array
{
return $this->success;
}
/**
* @return array
*/
public function getFakes(): array
{
return $this->fakes;
}
/**
* @return SymbolType
*/
public function getType(): SymbolType
{
return $this->type;
}
}