1 | <?php |
||
2 | |||
3 | namespace Mostafaznv\Larupload\Actions\Cover; |
||
4 | |||
5 | use Illuminate\Http\UploadedFile; |
||
6 | use Illuminate\Support\Facades\Storage; |
||
7 | use Mostafaznv\Larupload\Actions\SetFileNameAction; |
||
8 | use Mostafaznv\Larupload\DTOs\CoverActionData; |
||
9 | use Mostafaznv\Larupload\Enums\LaruploadFileType; |
||
10 | use Mostafaznv\Larupload\Storage\Image; |
||
11 | |||
12 | class UploadCoverAction |
||
13 | { |
||
14 | private array $output; |
||
15 | |||
16 | public function __construct(private readonly UploadedFile $cover, private readonly CoverActionData $data) |
||
17 | { |
||
18 | $this->output = $this->data->output; |
||
19 | } |
||
20 | |||
21 | public static function make(UploadedFile $cover, CoverActionData $data): static |
||
22 | { |
||
23 | return new static($cover, $data); |
||
24 | } |
||
25 | |||
26 | |||
27 | public function run(string $path): array |
||
28 | { |
||
29 | Storage::disk($this->data->disk)->makeDirectory($path); |
||
30 | |||
31 | $img = $this->img(); |
||
32 | $name = SetFileNameAction::make($this->cover, $this->data->namingMethod, $this->data->lang)->generate(); |
||
33 | $saveTo = "$path/$name"; |
||
34 | |||
35 | $result = $img->resize($saveTo, $this->data->style); |
||
36 | |||
37 | if ($result) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
38 | $this->output['cover'] = $name; |
||
39 | |||
40 | if ($this->data->type != LaruploadFileType::IMAGE) { |
||
41 | $this->output['dominant_color'] = $this->data->withDominantColor ? $img->getDominantColor() : null; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | return $this->output; |
||
46 | } |
||
47 | |||
48 | private function img(): Image |
||
49 | { |
||
50 | return new Image( |
||
51 | file: $this->cover, |
||
52 | disk: $this->data->disk, |
||
53 | library: $this->data->imageProcessingLibrary, |
||
54 | dominantColorQuality: $this->data->dominantColorQuality |
||
55 | ); |
||
56 | } |
||
57 | } |
||
58 |