1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace brendt\image; |
4
|
|
|
|
5
|
|
|
use brendt\image\exception\FileNotFoundException; |
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
9
|
|
|
|
10
|
|
|
class ResponsiveImage { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $src = ''; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string[] |
19
|
|
|
*/ |
20
|
|
|
private $srcset = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private $sizes = []; |
26
|
|
|
/** |
27
|
|
|
* ResponsiveImage constructor. |
28
|
|
|
* |
29
|
|
|
* @param $src |
30
|
|
|
*/ |
31
|
|
|
public function __construct($src) { |
32
|
|
|
$src = preg_replace('/^(\/|\.\/)/', '', $src); |
33
|
|
|
|
34
|
|
|
$this->src = "/{$src}"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function src() { |
41
|
|
|
return $this->src; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $sources |
46
|
|
|
* @param null $value |
47
|
|
|
* |
48
|
|
|
* @return ResponsiveImage |
49
|
|
|
*/ |
50
|
|
|
public function addSource($sources, $value = null) { |
51
|
|
|
if (!is_array($sources) && $value) { |
52
|
|
|
$sources = [$sources => $value]; |
53
|
|
|
} elseif (!is_array($sources)) { |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($sources as $url => $width) { |
58
|
|
|
$url = ltrim($url, '/'); |
59
|
|
|
$width = str_replace('px', '', $width); |
60
|
|
|
|
61
|
|
|
$this->srcset[$width] = "/{$url}"; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
krsort($this->srcset); |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function srcset() { |
73
|
|
|
$srcset = []; |
74
|
|
|
|
75
|
|
|
foreach ($this->srcset as $w => $url) { |
76
|
|
|
$srcset[] = "{$url} {$w}w"; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return implode(',', $srcset); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array|string $sizes |
84
|
|
|
* @param null $value |
85
|
|
|
* |
86
|
|
|
* @return ResponsiveImage |
87
|
|
|
*/ |
88
|
|
|
public function addSizes($sizes, $value = null) { |
89
|
|
|
if (!is_array($sizes) && $value) { |
90
|
|
|
$sizes = [$sizes => $value]; |
91
|
|
|
} elseif (!is_array($sizes)) { |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
foreach ($sizes as $media => $value) { |
96
|
|
|
$this->sizes[$media] = $value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function sizes() { |
106
|
|
|
$sizes = []; |
107
|
|
|
|
108
|
|
|
foreach ($this->sizes as $media => $value) { |
109
|
|
|
$media = rtrim(ltrim($media, '('), ')'); |
110
|
|
|
|
111
|
|
|
if (is_numeric($media)) { |
112
|
|
|
$sizes[] = "$value"; |
113
|
|
|
} else { |
114
|
|
|
$sizes[] = "({$media}) $value"; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return implode(', ', $sizes); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|