1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Image; |
4
|
|
|
|
5
|
|
|
use AsyncInterop\Promise; |
6
|
|
|
|
7
|
|
|
class ResponsiveImage |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $src = ''; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string[] |
17
|
|
|
*/ |
18
|
|
|
private $srcset = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string[] |
22
|
|
|
*/ |
23
|
|
|
private $sizes = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $fileName = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $extension = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $urlPath = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Promise |
42
|
|
|
*/ |
43
|
|
|
private $promise; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* ResponsiveImage constructor. |
47
|
|
|
* |
48
|
|
|
* @param $src |
49
|
|
|
*/ |
50
|
|
|
public function __construct($src) { |
51
|
|
|
$src = preg_replace('/^(\/|\.\/)/', '', $src); |
52
|
|
|
|
53
|
|
|
$this->src = "/{$src}"; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function src() { |
60
|
|
|
return $this->src; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $sources |
65
|
|
|
* @param null $value |
66
|
|
|
* |
67
|
|
|
* @return ResponsiveImage |
68
|
|
|
*/ |
69
|
|
|
public function addSource($sources, $value = null) { |
70
|
|
|
if (!is_array($sources) && $value) { |
71
|
|
|
$sources = [$sources => $value]; |
72
|
|
|
} elseif (!is_array($sources)) { |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
foreach ($sources as $url => $width) { |
77
|
|
|
$url = ltrim($url, '/'); |
78
|
|
|
$width = str_replace('px', '', $width); |
79
|
|
|
|
80
|
|
|
$this->srcset[$width] = "/{$url}"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
krsort($this->srcset); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function srcset() { |
92
|
|
|
$srcset = []; |
93
|
|
|
|
94
|
|
|
foreach ($this->srcset as $w => $url) { |
95
|
|
|
$srcset[] = "{$url} {$w}w"; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return implode(',', $srcset); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array|string $sizes |
103
|
|
|
* @param null $value |
104
|
|
|
* |
105
|
|
|
* @return ResponsiveImage |
106
|
|
|
*/ |
107
|
|
|
public function addSizes($sizes, $value = null) { |
108
|
|
|
if (!is_array($sizes) && $value) { |
109
|
|
|
$sizes = [$sizes => $value]; |
110
|
|
|
} elseif (!is_array($sizes)) { |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ($sizes as $media => $value) { |
115
|
|
|
$this->sizes[$media] = $value; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function sizes() { |
125
|
|
|
$sizes = []; |
126
|
|
|
|
127
|
|
|
foreach ($this->sizes as $media => $value) { |
128
|
|
|
$media = rtrim(ltrim($media, '('), ')'); |
129
|
|
|
|
130
|
|
|
if (is_numeric($media)) { |
131
|
|
|
$sizes[] = "$value"; |
132
|
|
|
} else { |
133
|
|
|
$sizes[] = "({$media}) $value"; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return implode(', ', $sizes); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Add a callback for when all image variants of this Responsive image are scaled and saved as files. |
142
|
|
|
* |
143
|
|
|
* @param callable $callback |
144
|
|
|
* |
145
|
|
|
* @return ResponsiveImage |
146
|
|
|
*/ |
147
|
|
|
public function onSave(callable $callback) : ResponsiveImage { |
148
|
|
|
if (!$this->promise) { |
149
|
|
|
$callback(); |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->promise->when($callback); |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Sets the promise which determines when all files are scaled and saved. |
161
|
|
|
* |
162
|
|
|
* @param Promise $promise |
163
|
|
|
* |
164
|
|
|
* @return ResponsiveImage |
165
|
|
|
*/ |
166
|
|
|
public function setPromise(Promise $promise) : ResponsiveImage { |
167
|
|
|
$this->promise = $promise; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the promise which determines when all files are scaled and saved. |
174
|
|
|
* |
175
|
|
|
* @return Promise|null |
176
|
|
|
*/ |
177
|
|
|
public function getPromise() : ?Promise { |
178
|
|
|
return $this->promise; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getFileName() { |
185
|
|
|
return $this->fileName; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $fileName |
190
|
|
|
*/ |
191
|
|
|
public function setFileName($fileName) { |
192
|
|
|
$this->fileName = $fileName; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function getExtension() { |
199
|
|
|
return $this->extension; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $extension |
204
|
|
|
*/ |
205
|
|
|
public function setExtension($extension) { |
206
|
|
|
$this->extension = $extension; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function getUrlPath() { |
213
|
|
|
return $this->urlPath; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $urlPath |
218
|
|
|
*/ |
219
|
|
|
public function setUrlPath($urlPath) { |
220
|
|
|
$this->urlPath = $urlPath; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return \string[] |
225
|
|
|
*/ |
226
|
|
|
public function getSrcset() { |
227
|
|
|
return $this->srcset; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return string |
232
|
|
|
*/ |
233
|
|
|
public function getSrc() { |
234
|
|
|
return $this->src; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
} |
238
|
|
|
|