1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace brendt\image; |
4
|
|
|
|
5
|
|
|
use brendt\image\config\ResponsiveFactoryConfigurator; |
6
|
|
|
use brendt\image\exception\FileNotFoundException; |
7
|
|
|
use Intervention\Image\Exception\NotReadableException; |
8
|
|
|
use Intervention\Image\ImageManager; |
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
10
|
|
|
use Symfony\Component\Finder\Finder; |
11
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
12
|
|
|
|
13
|
|
|
class ResponsiveFactory { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The image driver to use. |
17
|
|
|
* Available drivers: 'gd' and 'imagick'. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $driver; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The source path to load images from. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $sourcePath; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The public path to save rendered images. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $publicPath; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The minimum file size of generated images. |
39
|
|
|
* No image with a size less then this amount (in KB), will be generated. |
40
|
|
|
* |
41
|
|
|
* @var integer |
42
|
|
|
*/ |
43
|
|
|
protected $minSize; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Enabled cache will stop generated images from being overwritten. |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
private $enableCache; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* A percentage (between 0 and 1) to decrease image sizes with. |
54
|
|
|
* |
55
|
|
|
* @var float |
56
|
|
|
*/ |
57
|
|
|
protected $stepModifier; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The Intervention image engine. |
61
|
|
|
* |
62
|
|
|
* @var ImageManager |
63
|
|
|
*/ |
64
|
|
|
protected $engine; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var Filesystem |
68
|
|
|
*/ |
69
|
|
|
protected $fs; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* ResponsiveFactory constructor. |
73
|
|
|
* |
74
|
|
|
* @param ResponsiveFactoryConfigurator $configurator |
75
|
|
|
*/ |
76
|
|
|
public function __construct(ResponsiveFactoryConfigurator $configurator) { |
77
|
|
|
$configurator->configure($this); |
78
|
|
|
$this->sourcePath = rtrim($this->sourcePath, '/'); |
79
|
|
|
$this->publicPath = rtrim($this->publicPath, '/'); |
80
|
|
|
$this->engine = new ImageManager([ |
81
|
|
|
'driver' => $this->driver, |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$this->fs = new Filesystem(); |
85
|
|
|
if (!$this->fs->exists($this->publicPath)) { |
86
|
|
|
$this->fs->mkdir($this->publicPath); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $src |
92
|
|
|
* |
93
|
|
|
* @return ResponsiveImage |
94
|
|
|
* @throws FileNotFoundException |
95
|
|
|
*/ |
96
|
|
|
public function create($src) { |
97
|
|
|
$image = new ResponsiveImage($src); |
98
|
|
|
$src = $image->src(); |
99
|
|
|
$sourceImage = $this->getImageFile($this->sourcePath, $src); |
100
|
|
|
|
101
|
|
|
if (!$sourceImage) { |
102
|
|
|
throw new FileNotFoundException("{$this->sourcePath}{$src}"); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$publicImagePath = "{$this->publicPath}/{$src}"; |
106
|
|
|
|
107
|
|
|
if (!$this->enableCache || !$this->fs->exists($publicImagePath)) { |
108
|
|
|
if ($this->fs->exists($publicImagePath)) { |
109
|
|
|
$this->fs->remove($publicImagePath); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->fs->dumpFile($publicImagePath, $sourceImage->getContents()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$extension = $sourceImage->getExtension(); |
116
|
|
|
$name = str_replace(".{$extension}", '', $sourceImage->getFilename()); |
117
|
|
|
|
118
|
|
|
$urlParts = explode('/', $src); |
119
|
|
|
array_pop($urlParts); |
120
|
|
|
$urlPath = implode('/', $urlParts); |
121
|
|
|
|
122
|
|
|
$imageObject = $this->engine->make($sourceImage->getPathname()); |
123
|
|
|
$width = $imageObject->getWidth(); |
124
|
|
|
$height = $imageObject->getHeight(); |
125
|
|
|
$image->addSource($src, $width); |
126
|
|
|
|
127
|
|
|
$stepWidth = (int) ($width * $this->stepModifier); |
128
|
|
|
$stepHeight = (int) ($height * $this->stepModifier); |
129
|
|
|
$width -= $stepWidth; |
130
|
|
|
$height -= $stepHeight; |
131
|
|
|
|
132
|
|
|
while ($width >= $this->minSize) { |
133
|
|
|
$scaledName = "{$name}-{$width}.{$extension}"; |
134
|
|
|
$scaledSrc = "{$urlPath}/{$scaledName}"; |
135
|
|
|
$image->addSource($scaledSrc, $width); |
136
|
|
|
|
137
|
|
|
$publicScaledPath = "{$this->publicPath}/{$urlPath}/{$scaledName}"; |
138
|
|
|
if (!$this->enableCache || !$this->fs->exists($publicScaledPath)) { |
139
|
|
|
$this->fs->dumpFile( |
140
|
|
|
$publicScaledPath, |
141
|
|
|
$imageObject->resize($width, $height)->encode($extension) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$width -= $stepWidth; |
146
|
|
|
$height -= $stepHeight; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $image; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $directory |
154
|
|
|
* @param string $path |
155
|
|
|
* |
156
|
|
|
* @return SplFileInfo |
157
|
|
|
*/ |
158
|
|
|
private function getImageFile($directory, $path) { |
159
|
|
|
$iterator = Finder::create()->files()->in($directory)->path(ltrim($path, '/'))->getIterator(); |
160
|
|
|
$iterator->rewind(); |
161
|
|
|
|
162
|
|
|
return $iterator->current(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $driver |
167
|
|
|
* |
168
|
|
|
* @return ResponsiveFactory |
169
|
|
|
*/ |
170
|
|
|
public function setDriver($driver) { |
171
|
|
|
$this->driver = $driver; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $publicPath |
178
|
|
|
* |
179
|
|
|
* @return ResponsiveFactory |
180
|
|
|
*/ |
181
|
|
|
public function setPublicPath($publicPath) { |
182
|
|
|
$this->publicPath = $publicPath; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param boolean $enableCache |
189
|
|
|
* |
190
|
|
|
* @return ResponsiveFactory |
191
|
|
|
*/ |
192
|
|
|
public function setEnableCache($enableCache) { |
193
|
|
|
$this->enableCache = $enableCache; |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param int $minSize |
200
|
|
|
* |
201
|
|
|
* @return ResponsiveFactory |
202
|
|
|
*/ |
203
|
|
|
public function setMinSize($minSize) { |
204
|
|
|
$this->minSize = $minSize; |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param float $stepModifier |
211
|
|
|
* |
212
|
|
|
* @return ResponsiveFactory |
213
|
|
|
*/ |
214
|
|
|
public function setStepModifier($stepModifier) { |
215
|
|
|
$this->stepModifier = $stepModifier; |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $sourcePath |
222
|
|
|
* |
223
|
|
|
* @return ResponsiveFactory |
224
|
|
|
*/ |
225
|
|
|
public function setSourcePath($sourcePath) { |
226
|
|
|
$this->sourcePath = $sourcePath; |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
} |
232
|
|
|
|