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
|
|
|
* ResponsiveImage constructor. |
40
|
|
|
* |
41
|
|
|
* @param $src |
42
|
|
|
*/ |
43
|
|
|
public function __construct($src) { |
44
|
|
|
$src = preg_replace('/^(\/|\.\/)/', '', $src); |
45
|
|
|
|
46
|
|
|
$this->src = "/{$src}"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function src() { |
53
|
|
|
return $this->src; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $sources |
58
|
|
|
* @param null $value |
59
|
|
|
* |
60
|
|
|
* @return ResponsiveImage |
61
|
|
|
*/ |
62
|
|
|
public function addSource($sources, $value = null) { |
63
|
|
|
if (!is_array($sources) && $value) { |
64
|
|
|
$sources = [$sources => $value]; |
65
|
|
|
} elseif (!is_array($sources)) { |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($sources as $url => $width) { |
70
|
|
|
$url = ltrim($url, '/'); |
71
|
|
|
$width = str_replace('px', '', $width); |
72
|
|
|
|
73
|
|
|
$this->srcset[$width] = "/{$url}"; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
krsort($this->srcset); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function srcset() { |
85
|
|
|
$srcset = []; |
86
|
|
|
|
87
|
|
|
foreach ($this->srcset as $w => $url) { |
88
|
|
|
$srcset[] = "{$url} {$w}w"; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return implode(',', $srcset); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array|string $sizes |
96
|
|
|
* @param null $value |
97
|
|
|
* |
98
|
|
|
* @return ResponsiveImage |
99
|
|
|
*/ |
100
|
|
|
public function addSizes($sizes, $value = null) { |
101
|
|
|
if (!is_array($sizes) && $value) { |
102
|
|
|
$sizes = [$sizes => $value]; |
103
|
|
|
} elseif (!is_array($sizes)) { |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
foreach ($sizes as $media => $value) { |
108
|
|
|
$this->sizes[$media] = $value; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function sizes() { |
118
|
|
|
$sizes = []; |
119
|
|
|
|
120
|
|
|
foreach ($this->sizes as $media => $value) { |
121
|
|
|
$media = rtrim(ltrim($media, '('), ')'); |
122
|
|
|
|
123
|
|
|
if (is_numeric($media)) { |
124
|
|
|
$sizes[] = "$value"; |
125
|
|
|
} else { |
126
|
|
|
$sizes[] = "({$media}) $value"; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return implode(', ', $sizes); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getFileName() { |
137
|
|
|
return $this->fileName; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $fileName |
142
|
|
|
*/ |
143
|
|
|
public function setFileName($fileName) { |
144
|
|
|
$this->fileName = $fileName; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getExtension() { |
151
|
|
|
return $this->extension; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $extension |
156
|
|
|
*/ |
157
|
|
|
public function setExtension($extension) { |
158
|
|
|
$this->extension = $extension; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getUrlPath() { |
165
|
|
|
return $this->urlPath; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $urlPath |
170
|
|
|
*/ |
171
|
|
|
public function setUrlPath($urlPath) { |
172
|
|
|
$this->urlPath = $urlPath; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return \string[] |
177
|
|
|
*/ |
178
|
|
|
public function getSrcset() { |
179
|
|
|
return $this->srcset; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getSrc() { |
186
|
|
|
return $this->src; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
|