-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStyleManager.php
More file actions
301 lines (250 loc) · 9.25 KB
/
StyleManager.php
File metadata and controls
301 lines (250 loc) · 9.25 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* This file is part of the IrishDan\ResponsiveImageBundle package.
*
* (c) Daniel Byrne <danielbyrne@outlook.com>
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source
* code.
*/
namespace IrishDan\ResponsiveImageBundle;
use IrishDan\ResponsiveImageBundle\ImageProcessing\CoordinateGeometry;
/**
* Class StyleManager
* This class is responsible for image style information,
* and translating styles into relative style paths.
*
* @package ResponsiveImageBundle
*/
class StyleManager
{
private $breakpoints = [];
private $pictureSets = [];
private $sizeSets = [];
private $styles = [];
private $styleDirectory = 'styles';
/**
* StyleManager constructor.
*
* @param array $configuration
*/
public function __construct(array $configuration)
{
// Set the styles directory;
if (!empty($configuration['image_styles_directory'])) {
$this->styleDirectory = $configuration['image_styles_directory'];
}
// Set the styles array.
if (!empty($configuration['image_styles'])) {
$this->styles = $configuration['image_styles'];
}
// Set the picture sets array.
if (!empty($configuration['picture_sets'])) {
$this->pictureSets = $configuration['picture_sets'];
}
// Set the size sets array.
if (!empty($configuration['size_sets'])) {
$this->sizeSets = $configuration['size_sets'];
}
// Set the breakpoints array.
if (!empty($configuration['breakpoints'])) {
$this->breakpoints = $configuration['breakpoints'];
}
}
public function setImageAttributes(ResponsiveImageInterface $image, $styleName = null, $src = null)
{
// Use the style data to figure out the width and height for this image
// and then set hose attributes on the image.
if (!empty($styleName)) {
$styleData = $this->getStyleData($styleName);
if (!empty($styleData) && !empty($styleData['effect'])) {
switch ($styleData['effect']) {
case 'crop':
$image->setWidth($styleData['width']);
$image->setHeight($styleData['height']);
break;
case 'scale':
$scaledDimensions = $this->getScaledDimensions($image, $styleData);
$image->setWidth($scaledDimensions['width']);
$image->setHeight($scaledDimensions['height']);
break;
}
}
}
// Set the src if value is provided
if (!empty($src)) {
$image->setSrc($src);
}
return $image;
}
protected function getScaledDimensions(ResponsiveImageInterface $image, array $styleData)
{
$coordinates = $image->getCropCoordinates();
if (empty($coordinates)) {
$geometry = new CoordinateGeometry(0, 0, $image->getWidth(), $image->getHeight());
}
else {
$cropCoordinates = explode(':', $coordinates)[0];
$points = explode(',', $cropCoordinates);
$geometry = new CoordinateGeometry(
trim($points[0]),
trim($points[1]),
trim($points[2]),
trim($points[3])
);
}
return $geometry->scaleSize($styleData['width'], $styleData['height']);
}
public function styleExists($styleName)
{
// If its's a custom style, grab the data and add the styles array.
if (0 === strpos($styleName, 'custom_')) {
$styleData = $this->styleDataFromCustomStyleString($styleName);
$this->addStyle($styleName, $styleData);
}
$style = $this->getStyleData($styleName);
return !empty($style);
}
public function getAllStyles()
{
return $this->styles;
}
public function getAllStylesNames()
{
$styles = $this->getAllStyles();
return array_keys($styles);
}
public function getStyleData($styleName)
{
if (!in_array($styleName, array_keys($this->styles))) {
// If is custom style string.
if (strpos($styleName, 'custom_') === 0) {
return $this->styleDataFromCustomStyleString($styleName);
}
}
else {
return $this->styles[$styleName];
}
return false;
}
public function getPictureData(ResponsiveImageInterface $image, $pictureSetName)
{
$mappings = [
'fallback' => '',
'sources' => [],
];
$filename = $image->getPath();
if (!empty($this->pictureSets[$pictureSetName])) {
$set = $this->pictureSets[$pictureSetName];
foreach ($set['sources'] as $break => $styleName) {
$paths = [];
$paths[] = $this->buildStylePath($styleName, $filename);
// Check to for multiplier styles
$multiplierStyles = $this->findMultiplierStyles($styleName);
if (!empty($multiplierStyles)) {
foreach ($multiplierStyles as $multiplier => $style) {
$paths[] = $this->buildStylePath($style, $filename) . ' ' . $multiplier;
}
}
// Mappings should be in 'media_query' => '/path/to/image'
if ($this->breakpoints[$break]) {
$mediaQuery = $this->breakpoints[$break]['media_query'];
$mappings['sources'][$mediaQuery] = implode(', ', $paths);
}
}
// Set the fallback image path.
if (isset($set['fallback'])) {
$mappings['fallback'] = $this->getStylePath($image, $set['fallback']);
}
}
return $mappings;
}
protected function findMultiplierStyles($styleName)
{
$multiplierStyles = [];
foreach ($this->styles as $style => $styleData) {
// ^thumb_[0-9]+([.][0-9])?x$
$regex = '/^' . $styleName . '_([0-9]+([.][0-9])?x$)/';
preg_match($regex, $style, $matches);
if ($matches) {
$multiplierStyles[$matches[1]] = $style;
}
}
return $multiplierStyles;
}
public function getImageSizesData(ResponsiveImageInterface $image, $imageSizesSetName)
{
$mappings = [
'fallback' => '',
'sizes' => [],
'srcsets' => [],
];
$sizeData = $this->getSizesSet($imageSizesSetName);
if ($sizeData) {
// Sort out the sizes data.
$mappings['sizes'] = [];
foreach ($sizeData['sizes'] as $vw => $mediaQuery) {
// Get the media query from the breakpoint data.
$breakpoint = $this->breakpoints[$mediaQuery['breakpoint']];
if ($breakpoint) {
// $mappings['sizes'][$vw] = $breakpoint['media_query'];
$mappings['sizes'][] = '(' . $breakpoint['media_query'] . ') ' . $vw;
}
}
// Get the image paths and widths.
// In most case the width will be apart of the style (crop or scale)
// If it's not we can need to derive it.
foreach ($sizeData['srcsets'] as $styleName) {
$styleData = $this->getStyleData($styleName);
if ($styleData) {
if (empty($styleData['width'])) {
// We need to derive the width.
$scaledDimensions = $this->getScaledDimensions($image, $styleData);
$width = $scaledDimensions['width'];
}
else {
$width = $styleData['width'];
}
$path = $this->getStylePath($image, $styleName);
// Stick it into that array there.
$mappings['srcsets'][$path] = $width;
}
}
}
return $mappings;
}
protected function buildStylePath($styleName, $fileName)
{
$path = $this->styleDirectory . '/' . $styleName . '/' . $fileName;
return $path;
}
public function getStylePath(ResponsiveImageInterface $image, $styleName = null)
{
$filename = $image->getPath();
if (!empty($styleName)) {
$stylePath = $this->buildStylePath($styleName, $filename);
}
else {
$stylePath = $filename;
}
return $stylePath;
}
public function addStyle($key, $styleData)
{
$this->styles[$key] = $styleData;
}
public function styleDataFromCustomStyleString($customStyleString)
{
$styleData = explode('_', $customStyleString);
list($custom, $effect, $width, $height) = $styleData;
return [
'effect' => $effect,
'width' => $width,
'height' => $height,
];
}
public function getSizesSet($setName)
{
return isset($this->sizeSets[$setName]) ? $this->sizeSets[$setName] : false;
}
}