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\DTOs\CoverActionData; |
||
8 | use Mostafaznv\Larupload\Enums\LaruploadFileType; |
||
9 | use Mostafaznv\Larupload\Storage\FFMpeg\FFMpeg; |
||
10 | use Mostafaznv\Larupload\Storage\Image; |
||
11 | |||
12 | class GenerateCoverFromFileAction |
||
13 | { |
||
14 | private readonly string $fileName; |
||
15 | private readonly mixed $ffmpegCaptureFrame; |
||
16 | private array $output; |
||
17 | private array $availableStyles = [ |
||
18 | LaruploadFileType::VIDEO, LaruploadFileType::IMAGE |
||
19 | ]; |
||
20 | |||
21 | |||
22 | public function __construct(private readonly UploadedFile $file, private readonly CoverActionData $data) |
||
23 | { |
||
24 | $this->fileName = pathinfo($this->data->output['name'], PATHINFO_FILENAME); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
25 | $this->output = $this->data->output; |
||
26 | } |
||
27 | |||
28 | public static function make(UploadedFile $file, CoverActionData $data): static |
||
29 | { |
||
30 | return new static($file, $data); |
||
31 | } |
||
32 | |||
33 | |||
34 | public function run(string $path): array |
||
35 | { |
||
36 | if (!in_array($this->data->type, $this->availableStyles)) { |
||
37 | return $this->output; |
||
38 | } |
||
39 | |||
40 | Storage::disk($this->data->disk)->makeDirectory($path); |
||
41 | |||
42 | $format = $this->fileFormat(); |
||
43 | $name = "$this->fileName.$format"; |
||
44 | $saveTo = "$path/$name"; |
||
45 | |||
46 | switch ($this->data->type) { |
||
47 | case LaruploadFileType::VIDEO: |
||
48 | $this->generateCoverFromVideo($saveTo, $name); |
||
49 | break; |
||
50 | |||
51 | case LaruploadFileType::IMAGE: |
||
52 | $this->generateCoverFromImage($saveTo, $name); |
||
53 | break; |
||
54 | } |
||
55 | |||
56 | return $this->output; |
||
57 | } |
||
58 | |||
59 | private function generateCoverFromVideo(string $saveTo, string $name): void |
||
60 | { |
||
61 | $color = $this->ffmpeg()->capture( |
||
62 | $this->ffmpegCaptureFrame, $this->data->style, $saveTo, $this->data->withDominantColor |
||
63 | ); |
||
64 | |||
65 | $this->output['cover'] = $name; |
||
66 | $this->output['dominant_color'] = $color; |
||
67 | } |
||
68 | |||
69 | private function generateCoverFromImage(string $saveTo, string $name): void |
||
70 | { |
||
71 | $result = $this->img()->resize($saveTo, $this->data->style); |
||
72 | |||
73 | if ($result) { |
||
0 ignored issues
–
show
|
|||
74 | $this->output['cover'] = $name; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | private function fileFormat(): string |
||
79 | { |
||
80 | if ($this->data->type == LaruploadFileType::IMAGE) { |
||
81 | $format = $this->data->output['format']; |
||
82 | |||
83 | return $format == 'svg' ? 'png' : $format; |
||
84 | } |
||
85 | |||
86 | return 'jpg'; |
||
87 | } |
||
88 | |||
89 | private function ffmpeg(): FFMpeg |
||
90 | { |
||
91 | $this->ffmpegCaptureFrame = config('larupload.ffmpeg.capture-frame'); |
||
0 ignored issues
–
show
|
|||
92 | |||
93 | return new FFMpeg($this->file, $this->data->disk, $this->data->dominantColorQuality); |
||
94 | } |
||
95 | |||
96 | private function img(): Image |
||
97 | { |
||
98 | return new Image( |
||
99 | file: $this->file, |
||
100 | disk: $this->data->disk, |
||
101 | library: $this->data->imageProcessingLibrary, |
||
102 | dominantColorQuality: $this->data->dominantColorQuality |
||
103 | ); |
||
104 | } |
||
105 | } |
||
106 |