1
|
|
|
<?php |
2
|
|
|
namespace nstdio\svg\filter; |
3
|
|
|
|
4
|
|
|
use nstdio\svg\container\ContainerInterface; |
5
|
|
|
use nstdio\svg\ElementInterface; |
6
|
|
|
use nstdio\svg\traits\ElementTrait; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Filter |
10
|
|
|
* |
11
|
|
|
* @package nstdio\svg\filter |
12
|
|
|
* @author Edgar Asatryan <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Filter extends BaseFilter implements ContainerInterface |
15
|
|
|
{ |
16
|
|
|
use ElementTrait; |
17
|
|
|
|
18
|
24 |
|
public function __construct(ElementInterface $parent, $id = null) |
19
|
|
|
{ |
20
|
24 |
|
$defs = self::getDefs($parent); |
21
|
24 |
|
parent::__construct($defs); |
|
|
|
|
22
|
|
|
|
23
|
24 |
|
$this->id = $id; |
24
|
24 |
|
} |
25
|
|
|
|
26
|
24 |
|
public function getName() |
27
|
|
|
{ |
28
|
24 |
|
return 'filter'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Applies a drop shadow effect to the input image. A drop shadow is effectively a blurred, offset version of the |
33
|
|
|
* input image's alpha mask drawn in a particular color, composited below the image. The function accepts a |
34
|
|
|
* parameter of type <shadow> (defined in CSS3 Backgrounds), with the exception that the 'inset' keyword is not |
35
|
|
|
* allowed. This function is similar to the more established box-shadow property; the difference is that with |
36
|
|
|
* filters, some browsers provide hardware acceleration for better performance. The parameters of the <shadow> |
37
|
|
|
* parameter are as follows. |
38
|
|
|
* |
39
|
|
|
* @param ContainerInterface $container |
40
|
|
|
* @param string|float $offsetX These are two <length> values to set the shadow offset. <offset-x> |
41
|
|
|
* specifies the horizontal distance. Negative values place the shadow to |
42
|
|
|
* the left of the element. <offset-y> specifies the vertical distance. |
43
|
|
|
* Negative values place the shadow above the element. See <length> for |
44
|
|
|
* possible units. If both values are 0, the shadow is placed behind the |
45
|
|
|
* element (and may generate a blur effect if <blur-radius> and/or |
46
|
|
|
* <spread-radius> is set). |
47
|
|
|
* @param string|float $offsetY Same as offset. |
|
|
|
|
48
|
|
|
* @param string|float $blurRadius This is a third <length> value. The larger this value, the bigger the |
|
|
|
|
49
|
|
|
* blur, so the shadow becomes bigger and lighter. Negative values are not |
50
|
|
|
* allowed. If not specified, it will be 0 (the shadow's edge is sharp). |
51
|
|
|
* @param null $filterId |
52
|
|
|
* |
53
|
|
|
* @return Filter |
54
|
|
|
*/ |
55
|
1 |
|
public static function shadow(ContainerInterface $container, $offsetX, $offsetY = null, $blurRadius = null, $filterId = null) |
56
|
|
|
{ |
57
|
1 |
|
$filter = (new Filter($container, $filterId))->apply([ |
58
|
1 |
|
'x' => '-50%', |
59
|
1 |
|
'y' => '-50%', |
60
|
1 |
|
'width' => '200%', |
61
|
1 |
|
'height' => '200%', |
62
|
1 |
|
]); |
63
|
|
|
|
64
|
1 |
|
(new Offset($filter))->apply([ |
65
|
1 |
|
'result' => 'offOut', |
66
|
1 |
|
'in' => 'SourceGraphic', |
67
|
1 |
|
'dx' => $offsetX, |
68
|
1 |
|
'dy' => $offsetY === null ? $offsetX : $offsetY, |
69
|
1 |
|
]); |
70
|
|
|
|
71
|
1 |
|
(new ColorMatrix($filter))->apply([ |
72
|
1 |
|
'result' => 'matrixOut', |
73
|
1 |
|
'in' => 'offOut', |
74
|
1 |
|
'type' => 'matrix', |
75
|
1 |
|
'values' => '0.1 0 0 0 0 0 0.1 0 0 0 0 0 0.1 0 0 0 0 0 1 0', |
76
|
1 |
|
]); |
77
|
|
|
|
78
|
1 |
|
(new GaussianBlur($filter))->apply([ |
79
|
1 |
|
'result' => "blurOut", |
80
|
1 |
|
'stdDeviation' => $blurRadius, |
81
|
1 |
|
]); |
82
|
|
|
|
83
|
1 |
|
(new Blend($filter))->apply([ |
84
|
1 |
|
'in' => 'SourceGraphic', |
85
|
1 |
|
'in2' => 'blurOut', |
86
|
1 |
|
'mode' => 'normal', |
87
|
1 |
|
]); |
88
|
|
|
|
89
|
1 |
|
return $filter; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Converts the input image to grayscale. The value of 'amount' defines the proportion of the conversion. A value |
94
|
|
|
* of 100% is completely grayscale. A value of 0% leaves the input unchanged. Values between 0% and 100% are linear |
95
|
|
|
* multipliers on the effect. If the 'amount' parameter is missing, a value of 0 is used. |
96
|
|
|
* |
97
|
|
|
* @param ContainerInterface $container |
98
|
|
|
* @param int|string $amount |
99
|
|
|
* |
100
|
|
|
* @return Filter |
101
|
|
|
*/ |
102
|
1 |
|
public static function grayScale(ContainerInterface $container, $amount) |
103
|
|
|
{ |
104
|
1 |
|
$filter = new Filter($container); |
105
|
|
|
|
106
|
1 |
|
$amount = intval($amount); |
107
|
|
|
|
108
|
1 |
|
$amountR = 0.002126 * $amount; |
109
|
1 |
|
$amountG = 0.007152 * $amount; |
110
|
1 |
|
$amountB = 0.000722 * $amount; |
111
|
|
|
|
112
|
1 |
|
(new ColorMatrix($filter))->apply([ |
113
|
1 |
|
'type' => 'matrix', |
114
|
1 |
|
'values' => "$amountR $amountG $amountB 0 0 |
115
|
1 |
|
$amountR $amountG $amountB 0 0 |
116
|
1 |
|
$amountR $amountG $amountB 0 0 |
117
|
1 |
|
0 0 0 1 0", |
118
|
1 |
|
]); |
119
|
|
|
|
120
|
1 |
|
return $filter; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Inverts the samples in the input image. The value of 'amount' defines the proportion of the conversion. A value |
125
|
|
|
* of 100% is completely inverted. A value of 0% leaves the input unchanged. Values between 0% and 100% are linear |
126
|
|
|
* multipliers on the effect. If the 'amount' parameter is missing, a value of 0 is used. |
127
|
|
|
* |
128
|
|
|
* @param ContainerInterface $container |
129
|
|
|
* @param null $filterId |
130
|
|
|
* |
131
|
|
|
* @return Filter |
132
|
|
|
*/ |
133
|
1 |
|
public static function invert(ContainerInterface $container, $filterId = null) |
134
|
|
|
{ |
135
|
1 |
|
return ComponentTransfer::table($container, [[1, 0]], $filterId); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Converts the input image to sepia. The value of 'amount' defines the proportion of the conversion. A value of |
140
|
|
|
* 100% is completely sepia. A value of 0% leaves the input unchanged. Values between 0% and 100% are linear |
141
|
|
|
* multipliers on the effect. If the 'amount' parameter is missing, a value of 0 is used. |
142
|
|
|
* |
143
|
|
|
* @param ContainerInterface $container |
144
|
|
|
* @param $amount |
145
|
|
|
* @param null $filterId |
146
|
|
|
* |
147
|
|
|
* @return Filter |
148
|
|
|
*/ |
149
|
1 |
|
public static function sepia(ContainerInterface $container, $amount, $filterId = null) |
|
|
|
|
150
|
|
|
{ |
151
|
1 |
|
$filter = new Filter($container, $filterId); |
152
|
1 |
|
(new ColorMatrix($filter))->apply([ |
153
|
1 |
|
'type' => 'matrix', |
154
|
|
|
'values' => "0.393 0.769 0.189 0 0 |
155
|
|
|
0.349 0.686 0.168 0 0 |
156
|
|
|
0.272 0.534 0.131 0 0 |
157
|
1 |
|
0 0 0 1 0", |
158
|
1 |
|
]); |
159
|
|
|
|
160
|
1 |
|
return $filter; |
161
|
|
|
} |
162
|
|
|
} |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: