|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Brendt\Image; |
|
4
|
|
|
|
|
5
|
|
|
class ResponsiveImage |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @var string |
|
10
|
|
|
*/ |
|
11
|
|
|
private $src = ''; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string[] |
|
15
|
|
|
*/ |
|
16
|
|
|
private $srcset = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string[] |
|
20
|
|
|
*/ |
|
21
|
|
|
private $sizes = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $fileName = ''; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $extension = ''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $urlPath = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Promise |
|
40
|
|
|
*/ |
|
41
|
|
|
private $promise; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* ResponsiveImage constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param $src |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($src) { |
|
49
|
|
|
$src = preg_replace('/^(\/|\.\/)/', '', $src); |
|
50
|
|
|
|
|
51
|
|
|
$this->src = "/{$src}"; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
public function src() { |
|
58
|
|
|
return $this->src; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param $sources |
|
63
|
|
|
* @param null $value |
|
64
|
|
|
* |
|
65
|
|
|
* @return ResponsiveImage |
|
66
|
|
|
*/ |
|
67
|
|
|
public function addSource($sources, $value = null) { |
|
68
|
|
|
if (!is_array($sources) && $value) { |
|
69
|
|
|
$sources = [$sources => $value]; |
|
70
|
|
|
} elseif (!is_array($sources)) { |
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
foreach ($sources as $url => $width) { |
|
75
|
|
|
$url = ltrim($url, '/'); |
|
76
|
|
|
$width = str_replace('px', '', $width); |
|
77
|
|
|
|
|
78
|
|
|
$this->srcset[$width] = "/{$url}"; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
krsort($this->srcset); |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function srcset() { |
|
90
|
|
|
$srcset = []; |
|
91
|
|
|
|
|
92
|
|
|
foreach ($this->srcset as $w => $url) { |
|
93
|
|
|
$srcset[] = "{$url} {$w}w"; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return implode(',', $srcset); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param array|string $sizes |
|
101
|
|
|
* @param null $value |
|
102
|
|
|
* |
|
103
|
|
|
* @return ResponsiveImage |
|
104
|
|
|
*/ |
|
105
|
|
|
public function addSizes($sizes, $value = null) { |
|
106
|
|
|
if (!is_array($sizes) && $value) { |
|
107
|
|
|
$sizes = [$sizes => $value]; |
|
108
|
|
|
} elseif (!is_array($sizes)) { |
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
foreach ($sizes as $media => $value) { |
|
113
|
|
|
$this->sizes[$media] = $value; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public function sizes() { |
|
123
|
|
|
$sizes = []; |
|
124
|
|
|
|
|
125
|
|
|
foreach ($this->sizes as $media => $value) { |
|
126
|
|
|
$media = rtrim(ltrim($media, '('), ')'); |
|
127
|
|
|
|
|
128
|
|
|
if (is_numeric($media)) { |
|
129
|
|
|
$sizes[] = "$value"; |
|
130
|
|
|
} else { |
|
131
|
|
|
$sizes[] = "({$media}) $value"; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return implode(', ', $sizes); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getFileName() { |
|
142
|
|
|
return $this->fileName; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string $fileName |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setFileName($fileName) { |
|
149
|
|
|
$this->fileName = $fileName; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getExtension() { |
|
156
|
|
|
return $this->extension; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $extension |
|
161
|
|
|
*/ |
|
162
|
|
|
public function setExtension($extension) { |
|
163
|
|
|
$this->extension = $extension; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getUrlPath() { |
|
170
|
|
|
return $this->urlPath; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param string $urlPath |
|
175
|
|
|
*/ |
|
176
|
|
|
public function setUrlPath($urlPath) { |
|
177
|
|
|
$this->urlPath = $urlPath; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return \string[] |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getSrcset() { |
|
184
|
|
|
return $this->srcset; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getSrc() { |
|
191
|
|
|
return $this->src; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.