Passed
Push — master ( d7ad3b...186424 )
by Pascal
02:42
created

WatermarkFilter::getCommands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Filters;
4
5
use FFMpeg\Filters\Video\WatermarkFilter as FFMpegWatermarkFilter;
6
7
/**
8
 * Partly based on this PR:
9
 * https://github.com/PHP-FFMpeg/PHP-FFMpeg/pull/754/files
10
 */
11
class WatermarkFilter extends FFMpegWatermarkFilter
12
{
13
    protected $path;
14
15
    public function __construct($watermarkPath, array $coordinates = [], $priority = 0)
16
    {
17
        parent::__construct($watermarkPath, $coordinates, $priority);
18
19
        $this->path = $watermarkPath;
20
    }
21
22
    /**
23
     * Gets the commands from the base filter and normalizes the path.
24
     *
25
     * @return array
26
     */
27
    protected function getCommands()
28
    {
29
        $commands = parent::getCommands();
30
31
        $commands[1] = str_replace($this->path, static::normalizePath($this->path), $commands[1]);
32
33
        return $commands;
34
    }
35
36
    /**
37
     * Normalizes the path when running on Windows.
38
     *
39
     * @param string $path
40
     * @return string
41
     */
42
    public static function normalizePath(string $path): string
43
    {
44
        $path = windows_os() ? static::normalizeWindowsPath($path) : $path;
45
46
        return "'{$path}'";
47
    }
48
49
    /**
50
     * Replaces the slashes and escapes the colon.
51
     *
52
     * @param string $path
53
     * @return string
54
     */
55
    public static function normalizeWindowsPath(string $path): string
56
    {
57
        $path = str_replace('/', '\\', $path);
58
        $path = str_replace('\\', '\\\\', $path);
59
        $path = str_replace(':', '\\:', $path);
60
61
        return $path;
62
    }
63
}
64