1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate\Traits; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
trait ImageFilter |
10
|
|
|
{ |
11
|
|
|
private $filters = [ |
12
|
|
|
'IMG_FILTER_NEGATE', |
13
|
|
|
'IMG_FILTER_GRAYSCALE', |
14
|
|
|
'IMG_FILTER_BRIGHTNESS', |
15
|
|
|
'IMG_FILTER_CONTRAST', |
16
|
|
|
'IMG_FILTER_COLORIZE', |
17
|
|
|
'IMG_FILTER_EDGEDETECT', |
18
|
|
|
'IMG_FILTER_EMBOSS', |
19
|
|
|
'IMG_FILTER_GAUSSIAN_BLUR', |
20
|
|
|
'IMG_FILTER_SELECTIVE_BLUR', |
21
|
|
|
'IMG_FILTER_MEAN_REMOVAL', |
22
|
|
|
'IMG_FILTER_SMOOTH', |
23
|
|
|
'IMG_FILTER_PIXELATE' |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
public function asNegative() |
27
|
|
|
{ |
28
|
|
|
return $this->filter('IMG_FILTER_NEGATE'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function asGrayscale() |
32
|
|
|
{ |
33
|
|
|
return $this->filter('IMG_FILTER_GRAYSCALE'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setBrightness($level) |
37
|
|
|
{ |
38
|
|
|
return $this->filter('IMG_FILTER_BRIGHTNESS', $level); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setContrast($level) |
42
|
|
|
{ |
43
|
|
|
return $this->filter('IMG_FILTER_CONTRAST', $level); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function colorize($r, $g, $b, $alpha) |
47
|
|
|
{ |
48
|
|
|
$this->filter('IMG_FILTER_COLORIZE', $r, $g, $b, $alpha); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function detectEdge() |
54
|
|
|
{ |
55
|
|
|
return $this->filter('IMG_FILTER_EDGEDETECT'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function emboss() |
59
|
|
|
{ |
60
|
|
|
return $this->filter('IMG_FILTER_EMBOSS'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function blur() |
64
|
|
|
{ |
65
|
|
|
return $this->filter('IMG_FILTER_GAUSSIAN_BLUR'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function selectiveBlur() |
69
|
|
|
{ |
70
|
|
|
return $this->filter('IMG_FILTER_SELECTIVE_BLUR'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function meanRemoval() |
74
|
|
|
{ |
75
|
|
|
return $this->filter('IMG_FILTER_MEAN_REMOVAL'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function smooth($level) |
79
|
|
|
{ |
80
|
|
|
return $this->filter('IMG_FILTER_SMOOTH', $level); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function pixelate($size) |
84
|
|
|
{ |
85
|
|
|
return $this->filter('IMG_FILTER_PIXELATE', $size); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function filter() |
89
|
|
|
{ |
90
|
|
|
$args = func_get_args(); |
91
|
|
|
$filterType = array_shift($args); |
92
|
|
|
|
93
|
|
|
if (in_array($filterType, $this->filters)) { |
94
|
|
|
$params = [$this->source, constant($filterType)]; |
95
|
|
|
$params = array_values(array_merge($params, $args)); |
96
|
|
|
|
97
|
|
|
call_user_func_array('imagefilter', $params); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
throw new InvalidArgumentException( |
103
|
|
|
'Unknown filter type ' . $filterType |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|