-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathTrim.php
More file actions
139 lines (116 loc) · 2.86 KB
/
Copy pathTrim.php
File metadata and controls
139 lines (116 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace League\Glide\Manipulators;
use Intervention\Image\Image;
/**
* @property string $trim
*/
class Trim extends BaseManipulator
{
/**
* Perform trim image manipulation.
*
* @param Image $image The source image.
*
* @return Image The manipulated image.
*/
public function run(Image $image)
{
if ($trim = $this->getTrim()) {
list($base, $away, $tolerance, $feather) = $trim;
return $image->trim($base, $away, $tolerance, $feather);
}
return $image;
}
/**
* Resolve trim.
*
* @return array|null The resolved trim.
*/
public function getTrim()
{
if (!$this->trim) {
return;
}
$values = explode(',', $this->trim);
$base = $this->getBase(isset($values[0]) ? $values[0] : null);
$away = $this->getAway(isset($values[1]) ? $values[1] : null);
$tolerance = $this->getTolerance(isset($values[2]) ? $values[2] : null);
$feather = $this->getFeather(isset($values[3]) ? $values[3] : null);
return [$base, $away, $tolerance, $feather];
}
/**
* Resolve the base.
*
* @param string $base The raw base.
*
* @return string The resolved base.
*/
public function getBase($base)
{
if (!in_array($base, ['top-left', 'bottom-right', 'transparent'], true)) {
return 'top-left';
}
return $base;
}
/**
* Resolve the away.
*
* @param string $away The raw away.
*
* @return array|null The resolved away array.
*/
public function getAway($away)
{
if (null === $away || preg_match('/[^tblr]/', $away)) {
return;
}
$aways = [];
if (strpos($away, 't') !== false) {
$aways[] = 'top';
}
if (strpos($away, 'b') !== false) {
$aways[] = 'bottom';
}
if (strpos($away, 'l') !== false) {
$aways[] = 'left';
}
if (strpos($away, 'r') !== false) {
$aways[] = 'right';
}
if (empty($aways)) {
return;
}
return $aways;
}
/**
* Resolve the tolerance.
*
* @param string $tolerance The raw tolerance.
*
* @return int|null The resolved tolerance.
*/
public function getTolerance($tolerance)
{
if (!is_numeric($tolerance)) {
return;
}
if ($tolerance < 0 or $tolerance > 100) {
return;
}
return (int) $tolerance;
}
/**
* Resolve the feather.
*
* @param string $feather The raw feather.
*
* @return int|null The resolved feather.
*/
public function getFeather($feather)
{
if (!is_numeric($feather)) {
return;
}
return (int) $feather;
}
}