CropFilter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 23
c 2
b 0
f 0
dl 0
loc 62
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getFFmpegCLIValue() 0 14 7
A __construct() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @see       https://github.com/soluble-io/soluble-mediatools for the canonical repository
7
 *
8
 * @copyright Copyright (c) 2018-2020 Sébastien Vanvelthem. (https://github.com/belgattitude)
9
 * @license   https://github.com/soluble-io/soluble-mediatools/blob/master/LICENSE.md MIT
10
 */
11
12
namespace Soluble\MediaTools\Video\Filter;
13
14
use Soluble\MediaTools\Video\Filter\Type\FFMpegVideoFilterInterface;
15
16
final class CropFilter implements FFMpegVideoFilterInterface
17
{
18
    /** @var int|string|null */
19
    private $height;
20
21
    /** @var int|string|null */
22
    private $width;
23
24
    /** @var int|string|null */
25
    private $x;
26
27
    /** @var int|string|null */
28
    private $y;
29
30
    /** @var bool */
31
    private $keepAspect;
32
33
    /** @var bool */
34
    private $exact;
35
36
    /**
37
     * Crop filter.
38
     *
39
     * @see https://ffmpeg.org/ffmpeg-filters.html#crop
40
     *
41
     * @param string|int|null $width      The width of the output video, it defaults to 'iw'
42
     * @param string|int|null $height     The height of the output video, it defaults to 'ih'
43
     * @param string|int|null $x          The horizontal position, in the input video, of the left edge of the output video. It defaults to '(in_w-out_w)/2'
44
     * @param string|int|null $y          The vertical position, in the input video, of the top edge of the output video. It defaults to '(in_h-out_h)/2'
45
     * @param bool            $keepAspect will force the output display aspect ratio to be the same of the input, by changing the output sample aspect ratio
46
     * @param bool            $exact      Enable exact cropping. If enabled, subsampled videos will be cropped at exact width/height/x/y as specified and will not be rounded to nearest smaller value.
47
     */
48 1
    public function __construct(
49
        $width = null,
50
        $height = null,
51
        $x = null,
52
        $y = null,
53
        bool $keepAspect = false,
54
        bool $exact = false
55
    ) {
56 1
        $this->width      = $width;
57 1
        $this->height     = $height;
58 1
        $this->x          = $x;
59 1
        $this->y          = $y;
60 1
        $this->keepAspect = $keepAspect;
61 1
        $this->exact      = $exact;
62 1
    }
63
64 1
    public function getFFmpegCLIValue(): string
65
    {
66 1
        $args = array_filter([
67 1
            ($this->width !== null) ? "w={$this->width}" : false,
68 1
            ($this->height !== null) ? "h={$this->height}" : false,
69 1
            ($this->x !== null) ? "x={$this->x}" : false,
70 1
            ($this->y !== null) ? "y={$this->y}" : false,
71 1
            ($this->keepAspect) ? 'keep_aspect=1' : false,
72 1
            ($this->exact) ? 'exact=1' : false,
73
        ]);
74
75 1
        return sprintf(
76 1
            'crop=%s',
77 1
            implode(':', $args)
78
        );
79
    }
80
}
81