47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Jobs\Infrastructure\Files;
|
|
|
|
use App\Modules\Jobs\Application\Contracts\TaskImageFiles;
|
|
use App\Modules\Jobs\Domain\Exceptions\InvalidTaskImage;
|
|
use App\Modules\Jobs\Domain\ValueObjects\StoredTaskImage;
|
|
use App\Modules\Shared\Application\Contracts\ImageResizer;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Task image files on the private local disk: the image turned upright,
|
|
* scaled down and saved again as a JPEG without metadata, and a thumbnail.
|
|
*/
|
|
class GdTaskImageFiles implements TaskImageFiles
|
|
{
|
|
public const string DISK = 'local';
|
|
|
|
public const int MAX_SIZE = 2560;
|
|
|
|
public const int THUMBNAIL_SIZE = 400;
|
|
|
|
public function __construct(private ImageResizer $resizer) {}
|
|
|
|
public function store(string $uploadedFilePath, int $taskId): StoredTaskImage
|
|
{
|
|
$versions = $this->resizer->jpegVersions($uploadedFilePath, [self::MAX_SIZE, self::THUMBNAIL_SIZE]) ?? throw new InvalidTaskImage;
|
|
[$full, $thumbnail] = $versions;
|
|
|
|
$name = 'task-images/'.$taskId.'/'.Str::uuid()->toString();
|
|
$stored = new StoredTaskImage($name.'.jpg', $name.'-thumb.jpg', $full['width'], $full['height']);
|
|
|
|
Storage::disk(self::DISK)->put($stored->path, $full['bytes']);
|
|
Storage::disk(self::DISK)->put($stored->thumbnailPath, $thumbnail['bytes']);
|
|
|
|
return $stored;
|
|
}
|
|
|
|
public function delete(array $paths): void
|
|
{
|
|
if ($paths !== []) {
|
|
Storage::disk(self::DISK)->delete($paths);
|
|
}
|
|
}
|
|
}
|