composed project, added packages, models, controllers, seeders, mirgations etc.

This commit is contained in:
2024-07-28 17:45:09 +03:00
parent 5d05ee373a
commit 56eec65355
73 changed files with 3576 additions and 368 deletions

View File

@@ -0,0 +1,17 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-26
* @time: 19:44
*/
declare(strict_types=1);
namespace App\Services\Faker\Image\Contracts;
enum FakerImageProviderType
{
case LoremFlickr;
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-26
* @time: 19:51
*/
declare(strict_types=1);
namespace App\Services\Faker\Image\Contracts;
interface ProviderFactoryInterface
{
public static function apply(): ProviderInterface;
}

View File

@@ -0,0 +1,57 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-26
* @time: 19:48
*/
declare(strict_types=1);
namespace App\Services\Faker\Image\Contracts;
use App\Services\Faker\Image\ImageStorage;
interface ProviderInterface
{
public const array KEYWORDS
= [
'tournament',
'match',
'game',
'fight',
'championship',
'date',
'contest',
'competition',
'event',
'advent',
'meeting',
'adventure',
'convention',
'gathering',
'council',
'committee',
'seminar',
'congregation',
'symposium',
'congress',
'assembly',
'forum',
'colloquium',
'convocation',
'marvel',
'conference',
'celebration',
'holiday',
'ceremony',
];
/**
* @param ImageStorage $storage
*
* @return string|null
*/
public function getImage(ImageStorage $storage): ?string;
}

View File

@@ -0,0 +1,32 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-26
* @time: 19:56
*/
declare(strict_types=1);
namespace App\Services\Faker\Image;
use App\Services\Faker\Image\Providers\LoremFlickrFactory;
use App\Services\Faker\Image\Contracts\{ProviderFactoryInterface, FakerImageProviderType};
use RuntimeException;
class FakerImageProvider
{
public static function use(FakerImageProviderType $type): ProviderFactoryInterface
{
if ($type === FakerImageProviderType::LoremFlickr) {
{
return new LoremFlickrFactory();
}
} else {
{
throw new RuntimeException("Unknown image provider: " . $type->name);
}
}
}
}

View File

@@ -0,0 +1,70 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-26
* @time: 19:20
*/
//phpcs:ignore
declare(strict_types = 1);
namespace App\Services\Faker\Image;
use Illuminate\Support\Arr;
class ImageStorage
{
private string $destFolder;
private int $width;
private int $height;
private array $keywords;
public function __construct(string $destFolder, int $width, int $height, array $keywords = [])
{
$this->destFolder = $destFolder;
$this->width = $width;
$this->height = $height;
$this->keywords = $keywords;
}
/**
* @return string
*/
public function getFolder(): string
{
return $this->destFolder;
}
/**
* @return int
*/
public function getWidth(): int
{
return $this->width;
}
/**
* @return int
*/
public function getHeight(): int
{
return $this->height;
}
/**
* @param string $prefix
* @param string $suffix
*
* @return string|null
*/
public function getKeyword(string $prefix = '', string $suffix = ''): ?string
{
if (count($this->keywords) === 0) {
return null;
}
return $prefix . Arr::random($this->keywords) . $suffix;
}
}

View File

@@ -0,0 +1,69 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-26
* @time: 22:54
*/
//phpcs:ignore
declare(strict_types=1);
namespace App\Services\Faker\Image;
use App\Facades\Image;
use App\Services\Faker\Image\{Contracts\ProviderInterface};
use Error;
use Illuminate\Support\{Facades\Http, Facades\Storage, Str};
abstract class Provider implements ProviderInterface
{
public ?string $url = null;
abstract public function getImage(ImageStorage $storage): ?string;
/**
* @param ImageStorage $storage
* @param string|null $url
*
* @return string|null
*/
protected function download(ImageStorage $storage, ?string $url): ?string
{
$storagePath = Str::of($storage->getFolder()) . '/' . Str::uuid()->toString() . '.jpg';
$response = Http::get($url);
if ($response->successful() === false) {
appNotice("Couldn't download image from $url");
return null;
}
$result = Storage::disk("local")->put($storagePath, $response->body());
$verify = $this->verify($storagePath, config('image.faker.cropper'));
return $result && $verify !== "NULL" ? $verify : "NULL";
}
private function verify(string $storagePath, ?array $cropper = []): string
{
$fullPath = Image::localPath($storagePath);
if (is_null($fullPath)) {
appNotice("Couldn't locate image from $storagePath");
return "NULL";
}
$meta = Image::meta($fullPath);
if (empty($meta)) {
appNotice("Couldn't retrieve image meta from $fullPath");
return "NULL";
}
try {
return Image::cropAlign($fullPath, $storagePath, ...$cropper);
} catch (Error $err) {
//Image::log($err, LoggingSeverityLevelsType::emergency);
return "NULL";
}
}
}

View File

@@ -0,0 +1,25 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-26
* @time: 19:48
*/
declare(strict_types=1);
namespace App\Services\Faker\Image\Providers;
use App\Services\Faker\Image\{ImageStorage, Provider};
final class LoremFlickr extends Provider
{
public function getImage(ImageStorage $storage): ?string
{
$this->url = config('image.faker.provider.loremflickr.host') .
$storage->getWidth() . "/" . $storage->getHeight() . $storage->getKeyword('/');
return $this->download($storage, $this->url);
}
}

View File

@@ -0,0 +1,22 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-26
* @time: 20:03
*/
declare(strict_types=1);
namespace App\Services\Faker\Image\Providers;
use App\Services\Faker\Image\Contracts\{ProviderFactoryInterface, ProviderInterface};
class LoremFlickrFactory implements ProviderFactoryInterface
{
public static function apply(): ProviderInterface
{
return new LoremFlickr();
}
}

View File

@@ -0,0 +1,17 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-27
* @time: 12:58
*/
//phpcs:ignore
declare(strict_types=1);
namespace App\Services\Image\Contracts;
interface ImageInterface
{
}

View File

@@ -0,0 +1,77 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-27
* @time: 07:35
*/
//phpcs:ignore
declare(strict_types = 1);
namespace App\Services\Image;
use App\ConsoleStyleInterfaceType;
use App\LoggingSeverityLevelsType;
use App\Services\Image\Contracts\ImageInterface;
use App\Services\Image\ImageCropperTrait as Cropper;
use Exception;
use Illuminate\Support\Facades\{App, Log, Storage};
use Symfony\Component\Console\{Input\ArgvInput, Output\ConsoleOutput, Style\SymfonyStyle};
final class Image implements ImageInterface
{
use Cropper;
public static mixed $faker = null;
private SymfonyStyle $io;
private array $config;
private bool $isConsole;
private bool $isVerbosity;
private bool $isIO;
public function __construct()
{
$this->config = config('image');
}
/**
* @param string $imagePath
*
* @return string|null
*/
public function localPath(string $imagePath): ?string
{
$fullPath = realpath(Storage::path($imagePath));
if (! $fullPath || ! file_exists($fullPath)) {
return null;
}
return $fullPath;
}
/**
* @param string $fullPath
* @param string|null $property
*
* @return array|string
*/
public function meta(string $fullPath, ?string $property = null): array|string
{
try {
$meta = getimagesize($fullPath);
if (is_null($property) === false && array_key_exists($property, $this->config['meta'])) {
return $meta[$this->config['meta'][$property]];
}
return getimagesize($fullPath);
} catch (Exception $e) {
appWarning($e);
}
return [];
}
}

View File

@@ -0,0 +1,154 @@
<?php
/**
* @package: events-venues-task
* @author: Yevhen Odynets
* @date: 2024-07-27
* @time: 12:42
*/
declare(strict_types=1);
namespace App\Services\Image;
use Illuminate\Support\Facades\File;
use ReflectionException;
use RuntimeException;
trait ImageCropperTrait
{
public static int $counter = 1;
/**
* @throws ReflectionException
*/
public function cropAlign(
string $srcPath,
string $dbPath,
?int $cropWidth = null,
?int $cropHeight = null,
?string $horizontalAlign = 'center',
?string $verticalAlign = 'middle'
): string {
$resource = $this->imageResource($srcPath);
if ($resource === false) {
appNotice("could not get image resource: $srcPath");
return $dbPath;
}
[$image, $saveMethod, $quality] = $resource;
$width = imagesx($image);
$height = imagesy($image);
$cropWidth ??= $width;
$cropHeight ??= $height;
if (($width > $cropWidth || $height > $cropHeight) === false) {
$destPath = $this->destPath($srcPath, $dbPath, $width, $height);
appInfo(self::$counter++ . ". skip cropping ($width x $height): " . basename($destPath[0]) . " saved");
return File::move($srcPath, $destPath[0]) ? $destPath[1] : $dbPath;
}
$horizontalAlignPixels = $this->calculatePixelsForAlign($width, $cropWidth, $horizontalAlign);
$verticalAlignPixels = $this->calculatePixelsForAlign($height, $cropHeight, $verticalAlign);
$destPath = $this->destPath($srcPath, $dbPath, $horizontalAlignPixels[1], $verticalAlignPixels[1]);
$croppedImage = imagecrop($image, [
'x' => $horizontalAlignPixels[0],
'y' => $verticalAlignPixels[0],
'width' => $horizontalAlignPixels[1],
'height' => $verticalAlignPixels[1],
]);
if ($croppedImage !== false) {
$saveMethod($croppedImage, $destPath[0], $quality);
imagedestroy($croppedImage);
appSuccess(
self::$counter++ . ". cropped successfully from $width x $height: " . basename($destPath[0]) . " saved"
);
}
imagedestroy($image);
if (File::delete($srcPath) === false) {
$e = new RuntimeException("could not delete image: $srcPath");
appWarning($e);
return $dbPath;
}
return $destPath[1];
}
/**
* @param string $srcPath
*
* @return false|array
*/
private function imageResource(string $srcPath): false|array
{
return match (mime_content_type($srcPath)) {
'image/jpeg' => [imagecreatefromjpeg($srcPath), 'imagejpeg', 84],
'image/png' => [imagecreatefrompng($srcPath), 'imagepng', 7],
'image/gif' => [imagecreatefromgif($srcPath), 'imagegif', null],
default => false,
};
}
/**
* @param string $srcPath
* @param string $dbPath
* @param int $width
* @param int $height
*
* @return string[]
*/
private function destPath(string $srcPath, string $dbPath, int $width, int $height): array
{
$pi = pathinfo($srcPath);
$fs_path = sprintf(
"%s\%s-%dx%d.%s",
$pi['dirname'],
$pi['filename'],
$width,
$height,
$pi['extension']
);
$db_path = sprintf(
"%s/%s-%dx%d.%s",
dirname($dbPath),
$pi['filename'],
$width,
$height,
$pi['extension']
);
return [$fs_path, $db_path];
}
/**
* @param int $imageSize
* @param int $cropSize
* @param string $align
*
* @return array|int[]
*/
private function calculatePixelsForAlign(int $imageSize, int $cropSize, string $align): array
{
return match ($align) {
'left', 'top' => [0, min($cropSize, $imageSize)],
'right', 'bottom' => [max(0, $imageSize - $cropSize), min($cropSize, $imageSize)],
'center', 'middle' => [
max(0, floor(($imageSize / 2) - ($cropSize / 2))),
min($cropSize, $imageSize),
],
default => [0, $imageSize],
};
}
}