Add captcha image generation functionality.
This commit is contained in:
144
app/Captcha/Config/ImageBody.php
Normal file
144
app/Captcha/Config/ImageBody.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Captcha\Config;
|
||||
|
||||
use App\Captcha\Exceptions\CaptchaException;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
final readonly class ImageBody
|
||||
{
|
||||
public function __construct(
|
||||
private array $backgrounds,
|
||||
private array $fonts,
|
||||
private array $fontColors,
|
||||
private int $width = 300,
|
||||
private int $height = 240,
|
||||
private int $angle = 20,
|
||||
private array | int $fontSize = [20, 50],
|
||||
private int $numberLines = 3,
|
||||
private array $lineColors = []
|
||||
) {
|
||||
if ($this->width <= 0) {
|
||||
throw new CaptchaException('Incorrect $width settings.');
|
||||
}
|
||||
if ($this->height <= 0) {
|
||||
throw new CaptchaException('Incorrect $height settings.');
|
||||
}
|
||||
if (count($this->backgrounds) < 1) {
|
||||
throw new CaptchaException('Invalid $backgrounds parameter.');
|
||||
}
|
||||
if (count($this->fonts) < 1) {
|
||||
throw new CaptchaException('Invalid $fonts parameter.');
|
||||
}
|
||||
if (count($this->fontColors) < 1) {
|
||||
throw new CaptchaException('Invalid $fontColors parameter.');
|
||||
}
|
||||
if ($this->angle < 0) {
|
||||
throw new CaptchaException('Incorrect $angle settings.');
|
||||
}
|
||||
|
||||
if (!is_integer($this->fontSize) && !is_array($this->fontSize)) {
|
||||
throw new CaptchaException('The $fontSize parameter is not an array or integer.');
|
||||
} elseif (is_array($this->fontSize)) {
|
||||
if (count($this->fontSize) > 2) {
|
||||
throw new CaptchaException('The array of $fontSize parameters contains more than 2 keys.');
|
||||
}
|
||||
if ($this->fontSize[0] > $this->fontSize[1]) {
|
||||
throw new CaptchaException('The number of $fontSize[1] is less than $fontSize[0].');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBackgrounds(): array
|
||||
{
|
||||
return $this->backgrounds;
|
||||
}
|
||||
|
||||
public function randomBackground(): string
|
||||
{
|
||||
return Arr::random($this->getBackgrounds());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFonts(): array
|
||||
{
|
||||
return $this->fonts;
|
||||
}
|
||||
|
||||
public function randomFont(): string
|
||||
{
|
||||
return Arr::random($this->getFonts());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFontColors(): array
|
||||
{
|
||||
return $this->fontColors;
|
||||
}
|
||||
|
||||
public function randomFontColor(): string
|
||||
{
|
||||
return Arr::random($this->getFontColors());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getWidth(): int
|
||||
{
|
||||
return $this->width;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getHeight(): int
|
||||
{
|
||||
return $this->height;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getAngle(): int
|
||||
{
|
||||
return $this->angle;
|
||||
}
|
||||
|
||||
public function randomAngle(): int
|
||||
{
|
||||
return mt_rand($this->getAngle() * -1, $this->getAngle());
|
||||
}
|
||||
|
||||
public function getNumberLines(): int
|
||||
{
|
||||
return $this->numberLines;
|
||||
}
|
||||
|
||||
public function fontSize(): int
|
||||
{
|
||||
$fontSize = $this->fontSize;
|
||||
if (is_integer($fontSize)) {
|
||||
return $fontSize;
|
||||
}
|
||||
if (empty($fontSize[1])) {
|
||||
return $fontSize[0];
|
||||
}
|
||||
return random_int($fontSize[0], $fontSize[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getLineColors(): array
|
||||
{
|
||||
return $this->lineColors;
|
||||
}
|
||||
}
|
147
app/Captcha/Config/ImageHead.php
Normal file
147
app/Captcha/Config/ImageHead.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Captcha\Config;
|
||||
|
||||
use App\Captcha\Exceptions\CaptchaException;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
final readonly class ImageHead
|
||||
{
|
||||
public function __construct(
|
||||
private array $backgrounds,
|
||||
private array $fonts,
|
||||
private array $fontColors,
|
||||
private int $width = 150,
|
||||
private int $height = 40,
|
||||
private int $textPaddingTop = 5,
|
||||
private int $textPaddingLeft = 10,
|
||||
private int $angle = 20,
|
||||
private int $numberLines = 3,
|
||||
private array $lineColors = []
|
||||
) {
|
||||
if ($this->width <= 0) {
|
||||
throw new CaptchaException('Incorrect $width settings.');
|
||||
}
|
||||
if ($this->height <= 0) {
|
||||
throw new CaptchaException('Incorrect $height settings.');
|
||||
}
|
||||
if ($this->textPaddingTop < 0) {
|
||||
throw new CaptchaException('Incorrect $textPaddingTop settings.');
|
||||
}
|
||||
if ($this->textPaddingLeft < 0) {
|
||||
throw new CaptchaException('Incorrect $textPaddingLeft settings.');
|
||||
}
|
||||
if (count($this->backgrounds) < 1) {
|
||||
throw new CaptchaException('Invalid $backgrounds parameter.');
|
||||
}
|
||||
if (count($this->fonts) < 1) {
|
||||
throw new CaptchaException('Invalid $fonts parameter.');
|
||||
}
|
||||
if (count($this->fontColors) < 1) {
|
||||
throw new CaptchaException('Invalid $fontColors parameter.');
|
||||
}
|
||||
if ($this->angle < 0) {
|
||||
throw new CaptchaException('Incorrect $angle settings.');
|
||||
}
|
||||
if ($this->numberLines < 0) {
|
||||
throw new CaptchaException('Incorrect $numberLines settings.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getBackgrounds(): array
|
||||
{
|
||||
return $this->backgrounds;
|
||||
}
|
||||
|
||||
public function randomBackground(): string
|
||||
{
|
||||
return Arr::random($this->getBackgrounds());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFonts(): array
|
||||
{
|
||||
return $this->fonts;
|
||||
}
|
||||
|
||||
public function randomFont(): string
|
||||
{
|
||||
return Arr::random($this->getFonts());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFontColors(): array
|
||||
{
|
||||
return $this->fontColors;
|
||||
}
|
||||
|
||||
public function randomFontColor(): string
|
||||
{
|
||||
return Arr::random($this->getFontColors());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getWidth(): int
|
||||
{
|
||||
return $this->width;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getHeight(): int
|
||||
{
|
||||
return $this->height;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTextPaddingTop(): int
|
||||
{
|
||||
return $this->textPaddingTop;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTextPaddingLeft(): int
|
||||
{
|
||||
return $this->textPaddingLeft;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getAngle(): int
|
||||
{
|
||||
return $this->angle;
|
||||
}
|
||||
|
||||
public function randomAngle(): int
|
||||
{
|
||||
return mt_rand($this->getAngle() * -1, $this->getAngle());
|
||||
}
|
||||
|
||||
public function getNumberLines(): int
|
||||
{
|
||||
return $this->numberLines;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getLineColors(): array
|
||||
{
|
||||
return $this->lineColors;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user