1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mostafaznv\Larupload\DTOs\Style; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use FFMpeg\Format\Video\X264; |
7
|
|
|
use Mostafaznv\Larupload\Enums\LaruploadMediaStyle; |
8
|
|
|
|
9
|
|
|
class VideoStyle extends Style |
10
|
|
|
{ |
11
|
|
|
public readonly LaruploadMediaStyle $mode; |
12
|
|
|
public readonly X264 $format; |
13
|
|
|
|
14
|
|
|
public function __construct(string $name, ?int $width = null, ?int $height = null, LaruploadMediaStyle $mode = LaruploadMediaStyle::SCALE_HEIGHT, X264 $format = new X264, bool $padding = false) |
15
|
|
|
{ |
16
|
|
|
parent::__construct($name, $width, $height, $padding); |
17
|
|
|
|
18
|
|
|
$this->mode = $mode; |
|
|
|
|
19
|
|
|
$this->format = $format; |
|
|
|
|
20
|
|
|
|
21
|
|
|
$this->validateDimension(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function make(string $name, ?int $width = null, ?int $height = null, LaruploadMediaStyle $mode = LaruploadMediaStyle::SCALE_HEIGHT, X264 $format = new X264, bool $padding = false): self |
25
|
|
|
{ |
26
|
|
|
return new self($name, $width, $height, $mode, $format, $padding); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
private function validateDimension(): void |
31
|
|
|
{ |
32
|
|
|
if ($this->mode === LaruploadMediaStyle::SCALE_HEIGHT) { |
33
|
|
|
if ($this->width === null or $this->width === 0) { |
34
|
|
|
throw new Exception( |
35
|
|
|
'Width is required when you are in SCALE_HEIGHT mode' |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
else if ($this->mode === LaruploadMediaStyle::SCALE_WIDTH) { |
40
|
|
|
if ($this->height === null or $this->height === 0) { |
41
|
|
|
throw new Exception( |
42
|
|
|
'Height is required when you are in SCALE_WIDTH mode' |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
else if (in_array($this->mode, [LaruploadMediaStyle::CROP, LaruploadMediaStyle::FIT])) { |
47
|
|
|
if (!$this->width or !$this->height) { |
48
|
|
|
throw new Exception( |
49
|
|
|
'Width and Height are required when you are in CROP/FIT mode' |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
else if ($this->mode === LaruploadMediaStyle::AUTO) { |
54
|
|
|
if (!$this->width and !$this->height) { |
55
|
|
|
throw new Exception( |
56
|
|
|
'Width and height are required when you are in auto mode' |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|