composed project, added packages, models, controllers, seeders, mirgations etc.
This commit is contained in:
69
app/Services/Faker/Image/Provider.php
Normal file
69
app/Services/Faker/Image/Provider.php
Normal 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";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user