1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the -SeamsCMSDeliverySdk package. |
7
|
|
|
* |
8
|
|
|
* (c) Seams-CMS.com |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace SeamsCMS\Delivery; |
15
|
|
|
|
16
|
|
|
use SeamsCMS\Delivery\Model\Asset; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ImageBuilder |
20
|
|
|
* @package SeamsCMS\Delivery |
21
|
|
|
* |
22
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
23
|
|
|
*/ |
24
|
|
|
class ImageBuilder |
25
|
|
|
{ |
26
|
|
|
const ASSET_BASE_URL_CDN = "https://assets.seams-cms.com"; |
27
|
|
|
const ASSET_BASE_URL = "https://assets-nocdn.seams-cms.com"; |
28
|
|
|
|
29
|
|
|
const CROP_TOP_LEFT = "topleft"; |
30
|
|
|
const CROP_TOP = "top"; |
31
|
|
|
const CROP_TOP_RIGHT = "topright"; |
32
|
|
|
const CROP_LEFT = "left"; |
33
|
|
|
const CROP_CENTER = "center"; |
34
|
|
|
const CROP_RIGHT = "right"; |
35
|
|
|
const CROP_BOTTOM_LEFT = "bottomleft"; |
36
|
|
|
const CROP_BOTTOM = "bottom"; |
37
|
|
|
const CROP_BOTTOM_RIGHT = "bottomright"; |
38
|
|
|
|
39
|
|
|
const FLIP_HORIZONTAL = "horizontal"; |
40
|
|
|
const FLIP_VERTICAL = "vertical"; |
41
|
|
|
const FLIP_BOTH = "both"; |
42
|
|
|
|
43
|
|
|
/** @var array */ |
44
|
|
|
protected $filters = array(); |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** @var string */ |
48
|
|
|
protected $workspace; |
49
|
|
|
/** @var string */ |
50
|
|
|
protected $path; |
51
|
|
|
|
52
|
|
|
/** @var bool */ |
53
|
|
|
protected $cdn = false; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Asset $asset |
57
|
|
|
* @return ImageBuilder |
58
|
|
|
*/ |
59
|
4 |
|
public static function fromAsset(Asset $asset) |
60
|
|
|
{ |
61
|
4 |
|
return new self($asset->getWorkspace(), $asset->getPath()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $workspace |
66
|
|
|
* @param string $path |
67
|
|
|
* @return ImageBuilder |
68
|
|
|
*/ |
69
|
1 |
|
public static function fromPath(string $workspace, string $path) |
70
|
|
|
{ |
71
|
1 |
|
return new self($workspace, $path); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* ImageBuilder constructor. |
76
|
|
|
* |
77
|
|
|
* @param string $workspace |
78
|
|
|
* @param string $path |
79
|
|
|
*/ |
80
|
4 |
|
protected function __construct(string $workspace, string $path) |
81
|
|
|
{ |
82
|
4 |
|
$this->workspace = $workspace; |
83
|
4 |
|
$this->path = $path; |
84
|
|
|
|
85
|
4 |
|
$this->cdn = true; |
86
|
4 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Skips the CDN host and uses the direct asset API. |
90
|
|
|
* |
91
|
|
|
* Note that this should not be used for production purposes. |
92
|
|
|
* |
93
|
|
|
* @return ImageBuilder |
94
|
|
|
*/ |
95
|
3 |
|
public function skipCdn(): ImageBuilder |
96
|
|
|
{ |
97
|
3 |
|
$this->cdn = false; |
98
|
|
|
|
99
|
3 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Uses the CDN host. |
104
|
|
|
* |
105
|
|
|
* @return self |
106
|
|
|
*/ |
107
|
2 |
|
public function useCdn(): self |
108
|
|
|
{ |
109
|
2 |
|
$this->cdn = true; |
110
|
|
|
|
111
|
2 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return ImageBuilder |
116
|
|
|
*/ |
117
|
2 |
|
public function blur(): self |
118
|
|
|
{ |
119
|
2 |
|
$this->filters[] = 'blur'; |
120
|
|
|
|
121
|
2 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param int $red |
126
|
|
|
* @param int $green |
127
|
|
|
* @param int $blue |
128
|
|
|
* @param int $alpha |
129
|
|
|
* @return ImageBuilder |
130
|
|
|
*/ |
131
|
2 |
|
public function colorize(int $red, int $green, int $blue, int $alpha): self |
132
|
|
|
{ |
133
|
2 |
|
$this->filters[] = sprintf('colorize(%d,%d,%d,%d)', $red, $green, $blue, $alpha); |
134
|
|
|
|
135
|
2 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $position One of the CROP_* constants |
140
|
|
|
* @param int $width |
141
|
|
|
* @param int $height |
142
|
|
|
* @return ImageBuilder |
143
|
|
|
*/ |
144
|
1 |
|
public function crop(string $position = self::CROP_CENTER, int $width = 100, int $height = 100): self |
145
|
|
|
{ |
146
|
1 |
|
$this->filters[] = sprintf('crop(%s,%d,%d)', $position, $width, $height); |
147
|
|
|
|
148
|
1 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return ImageBuilder |
153
|
|
|
*/ |
154
|
1 |
|
public function cropsides(): self |
155
|
|
|
{ |
156
|
1 |
|
$this->filters[] = 'cropsides'; |
157
|
|
|
|
158
|
1 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $direction One of the FLIP_* constants |
163
|
|
|
* @return ImageBuilder |
164
|
|
|
*/ |
165
|
1 |
|
public function flip(string $direction = self::FLIP_HORIZONTAL): self |
166
|
|
|
{ |
167
|
1 |
|
$this->filters[] = sprintf('flip(%s)', $direction); |
168
|
|
|
|
169
|
1 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return ImageBuilder |
174
|
|
|
*/ |
175
|
2 |
|
public function gray(): self |
176
|
|
|
{ |
177
|
2 |
|
$this->filters[] = 'gray'; |
178
|
|
|
|
179
|
2 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param int $height |
184
|
|
|
* @return ImageBuilder |
185
|
|
|
*/ |
186
|
2 |
|
public function height(int $height): self |
187
|
|
|
{ |
188
|
2 |
|
$this->filters[] = sprintf('height(%d)', $height); |
189
|
|
|
|
190
|
2 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return ImageBuilder |
195
|
|
|
*/ |
196
|
2 |
|
public function negate(): self |
197
|
|
|
{ |
198
|
2 |
|
$this->filters[] = 'negate'; |
199
|
|
|
|
200
|
2 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param int $angle |
205
|
|
|
* @return ImageBuilder |
206
|
|
|
*/ |
207
|
2 |
|
public function rotate(int $angle): self |
208
|
|
|
{ |
209
|
2 |
|
$this->filters[] = sprintf('rotate(%d)', $angle); |
210
|
|
|
|
211
|
2 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param int $width |
216
|
|
|
* @return ImageBuilder |
217
|
|
|
*/ |
218
|
1 |
|
public function width(int $width): self |
219
|
|
|
{ |
220
|
1 |
|
$this->filters[] = sprintf('width(%d)', $width); |
221
|
|
|
|
222
|
1 |
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Returns the actual URL to the image. |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
4 |
|
public function getSourceUrl(): string |
231
|
|
|
{ |
232
|
4 |
|
$url = sprintf("%s/%s", $this->workspace, $this->path); |
233
|
|
|
|
234
|
|
|
// Add filters if any are found |
235
|
4 |
|
if (count($this->filters) > 0) { |
236
|
2 |
|
$sortedFilters = $this->filters; |
237
|
2 |
|
sort($sortedFilters); |
238
|
|
|
|
239
|
2 |
|
$url = sprintf('p/%s/%s', join('/', $sortedFilters), $url); |
240
|
|
|
} |
241
|
|
|
|
242
|
4 |
|
return sprintf("%s/%s", $this->cdn ? self::ASSET_BASE_URL_CDN : self::ASSET_BASE_URL, $url); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|