145 lines
3.4 KiB
PHP
145 lines
3.4 KiB
PHP
<?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;
|
|
}
|
|
}
|