Add captcha image generation functionality.
This commit is contained in:
97
app/Services/Api/V1/CaptchaService.php
Normal file
97
app/Services/Api/V1/CaptchaService.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace App\Services\Api\V1;
|
||||
|
||||
use App\Captcha\Contracts\ImageBody;
|
||||
use App\Captcha\Contracts\ImageHead;
|
||||
use App\Captcha\Contracts\Type;
|
||||
use App\ServiceResults\Api\V1\CaptchaService\Captcha;
|
||||
use App\ServiceResults\ServiceResultError;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Arr;
|
||||
use App\Captcha\Config\ImageHead as ImageHeadConfig;
|
||||
use App\Captcha\Config\ImageBody as ImageBodyConfig;
|
||||
|
||||
final class CaptchaService extends Service
|
||||
{
|
||||
public function __construct(
|
||||
private readonly array $config,
|
||||
private readonly ImageHead $imageHead,
|
||||
private readonly ImageBody $imageBody
|
||||
) { }
|
||||
|
||||
public function generate(): ServiceResultError | Captcha
|
||||
{
|
||||
$types = $this->config['types'] ?? [];
|
||||
if (empty($types)) {
|
||||
$error = __('No captcha type settings!');
|
||||
report($error);
|
||||
return $this->errService($error);
|
||||
}
|
||||
|
||||
try {
|
||||
$type = Arr::random($types);
|
||||
|
||||
/** @var Type $captcha */
|
||||
$typeCaptcha = new $type['class'](
|
||||
$type['params'] ?? []
|
||||
);
|
||||
$symbols = $typeCaptcha->getSymbols();
|
||||
|
||||
$imageHeadConfig = $this->makeImageHeadConfig($this->config['image_head'] ?? []);
|
||||
$imageHead = $this->imageHead->processing($symbols, $imageHeadConfig);
|
||||
unset($imageHeadConfig);
|
||||
|
||||
$imageBodyConfig = $this->makeImageBodyConfig($this->config['image_body'] ?? []);
|
||||
$imageBody = $this->imageBody->processing($symbols, $imageBodyConfig);
|
||||
unset($imageBodyConfig);
|
||||
|
||||
|
||||
dd($imageHead, $imageBody);
|
||||
} catch (\Throwable $e) {
|
||||
report($e);
|
||||
return $this->errService('Captcha service error!');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return new Captcha(
|
||||
imageBase64: $imageHead->getImage()->getImageBase64(),
|
||||
imageTextBase64: $imageBody->getImage()->getImageBase64(),
|
||||
key: 'dddd'
|
||||
);
|
||||
}
|
||||
|
||||
private function makeImageHeadConfig(array $params): ImageHeadConfig
|
||||
{
|
||||
return new ImageHeadConfig(
|
||||
backgrounds: $params['backgrounds'] ?? [],
|
||||
fonts: $params['fonts'] ?? [],
|
||||
fontColors: $params['font_colors'] ?? [],
|
||||
width: $params['width'] ?? 150,
|
||||
height: $params['height'] ?? 40,
|
||||
textPaddingTop: $params['text_padding_top'] ?? 5,
|
||||
textPaddingLeft: $params['text_padding_left'] ?? 10,
|
||||
angle: $params['angle'] ?? 20,
|
||||
numberLines: $params['number_lines'] ?? 3,
|
||||
lineColors: $params['line_colors'] ?? []
|
||||
);
|
||||
}
|
||||
|
||||
private function makeImageBodyConfig(array $params): ImageBodyConfig
|
||||
{
|
||||
return new ImageBodyConfig(
|
||||
backgrounds: $params['backgrounds'] ?? [],
|
||||
fonts: $params['fonts'] ?? [],
|
||||
fontColors: $params['font_colors'] ?? [],
|
||||
width: $params['width'] ?? 300,
|
||||
height: $params['height'] ?? 240,
|
||||
angle: $params['angle'] ?? 20,
|
||||
fontSize: $params['font_size'] ?? [20, 50],
|
||||
numberLines: $params['number_lines'] ?? 3,
|
||||
lineColors: $params['line_colors'] ?? []
|
||||
);
|
||||
}
|
||||
}
|
54
app/Services/Service.php
Normal file
54
app/Services/Service.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\ServiceResults\ServiceResultArray;
|
||||
use App\ServiceResults\ServiceResultError;
|
||||
|
||||
abstract class Service
|
||||
{
|
||||
final protected function errValidate(string $message, array $errors = []): ServiceResultError
|
||||
{
|
||||
return $this->error(422, $message, $errors);
|
||||
}
|
||||
|
||||
final protected function errFobidden(string $message): ServiceResultError
|
||||
{
|
||||
return $this->error(403, $message);
|
||||
}
|
||||
|
||||
final protected function errNotFound(string $message): ServiceResultError
|
||||
{
|
||||
return $this->error(404, $message);
|
||||
}
|
||||
|
||||
final protected function errService(string $message): ServiceResultError
|
||||
{
|
||||
return $this->error(500, $message);
|
||||
}
|
||||
|
||||
final protected function notAcceptable(string $message): ServiceResultError
|
||||
{
|
||||
return $this->error(406, $message);
|
||||
}
|
||||
|
||||
final protected function ok(string $message = 'OK'): ServiceResultArray
|
||||
{
|
||||
return $this->result(['message' => $message]);
|
||||
}
|
||||
|
||||
final protected function result(array $data = []): ServiceResultArray
|
||||
{
|
||||
return new ServiceResultArray(data: $data);
|
||||
}
|
||||
|
||||
final protected function error(int $code, string $message, array $errors = []): ServiceResultError
|
||||
{
|
||||
return new ServiceResultError(
|
||||
message: $message,
|
||||
errors: $errors,
|
||||
code: $code
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user