1 | <?php |
||
16 | class ResponsiveFactory |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * The image driver to use. |
||
21 | * Available drivers: 'gd' and 'imagick'. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $driver; |
||
26 | |||
27 | /** |
||
28 | * The source path to load images from. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $sourcePath; |
||
33 | |||
34 | /** |
||
35 | * The public path to save rendered images. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $publicPath; |
||
40 | |||
41 | /** |
||
42 | * Enabled cache will stop generated images from being overwritten. |
||
43 | * |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $enableCache; |
||
47 | |||
48 | /** |
||
49 | * Enable optimizers will run several image optimizers on the saved files. |
||
50 | * |
||
51 | * @var bool |
||
52 | */ |
||
53 | private $optimize; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $rebase; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | private $optimizerOptions = []; |
||
64 | |||
65 | /** |
||
66 | * The Intervention image engine. |
||
67 | * |
||
68 | * @var ImageManager |
||
69 | */ |
||
70 | protected $engine; |
||
71 | |||
72 | /** |
||
73 | * @var Filesystem |
||
74 | */ |
||
75 | protected $fs; |
||
76 | |||
77 | /** |
||
78 | * @var Scaler |
||
79 | */ |
||
80 | protected $scaler; |
||
81 | |||
82 | /** |
||
83 | * @var Optimizer |
||
84 | */ |
||
85 | protected $optimizer; |
||
86 | |||
87 | /** |
||
88 | * ResponsiveFactory constructor. |
||
89 | * |
||
90 | * @param ResponsiveFactoryConfigurator $configurator |
||
91 | */ |
||
92 | public function __construct(ResponsiveFactoryConfigurator $configurator = null) { |
||
110 | |||
111 | /** |
||
112 | * @param string $src |
||
113 | * |
||
114 | * @return ResponsiveImage |
||
115 | * @throws FileNotFoundException |
||
116 | */ |
||
117 | public function create($src) { |
||
118 | $responsiveImage = new ResponsiveImage($src); |
||
119 | $src = $responsiveImage->src(); |
||
120 | $sourceFilename = $this->rebase ? pathinfo($src, PATHINFO_BASENAME) : $src; |
||
121 | $sourceFile = $this->getImageFile($this->sourcePath, $sourceFilename); |
||
122 | |||
123 | $filename = pathinfo($sourceFile->getFilename(), PATHINFO_FILENAME); |
||
124 | $urlPath = '/' . trim(pathinfo($src, PATHINFO_DIRNAME), '/'); |
||
125 | |||
126 | $responsiveImage->setExtension($sourceFile->getExtension()); |
||
127 | $responsiveImage->setFileName($filename); |
||
128 | $responsiveImage->setUrlPath($urlPath); |
||
129 | |||
130 | if ($cachedResponsiveImage = $this->getCachedResponsiveImage($responsiveImage, $sourceFile)) { |
||
|
|||
131 | return $cachedResponsiveImage; |
||
132 | } |
||
133 | |||
134 | $this->fs->dumpFile("{$this->publicPath}{$src}", $sourceFile->getContents()); |
||
135 | $this->createScaledImages($sourceFile, $responsiveImage); |
||
136 | |||
137 | return $responsiveImage; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param ResponsiveImage $responsiveImage |
||
142 | * @param SplFileInfo $imageFile |
||
143 | * |
||
144 | * @return ResponsiveImage|null |
||
145 | */ |
||
146 | public function getCachedResponsiveImage(ResponsiveImage $responsiveImage, SplFileInfo $imageFile) { |
||
147 | $src = $responsiveImage->src(); |
||
148 | $publicImagePath = "{$this->publicPath}{$src}"; |
||
149 | |||
150 | if ($this->enableCache && $this->fs->exists($publicImagePath)) { |
||
151 | $extension = $imageFile->getExtension(); |
||
152 | $publicDirectory = $this->rebase ? trim(pathinfo($src, PATHINFO_DIRNAME), '/') : $imageFile->getRelativePath(); |
||
153 | $imageFilename = pathinfo($imageFile->getFilename(), PATHINFO_FILENAME); |
||
154 | |||
155 | /** @var SplFileInfo[] $cachedFiles */ |
||
156 | $cachedFiles = Finder::create()->files()->in("{$this->publicPath}/{$publicDirectory}")->name("{$imageFilename}-*.{$extension}"); |
||
157 | |||
158 | foreach ($cachedFiles as $cachedFile) { |
||
159 | $cachedFilename = $cachedFile->getFilename(); |
||
160 | $size = (int) str_replace(".{$extension}", '', str_replace("{$imageFilename}-", '', $cachedFilename)); |
||
161 | |||
162 | $responsiveImage->addSource("{$responsiveImage->getUrlPath()}/{$cachedFilename}", $size); |
||
163 | } |
||
164 | |||
165 | return $responsiveImage; |
||
166 | } |
||
167 | |||
168 | return null; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Create scaled image files and add them as sources to a Responsive Image, based on an array of file sizes: |
||
173 | * [ |
||
174 | * width => height, |
||
175 | * ... |
||
176 | * ] |
||
177 | * |
||
178 | * @param SplFileInfo $sourceImage |
||
179 | * @param ResponsiveImage $responsiveImage |
||
180 | * |
||
181 | * @return ResponsiveImage |
||
182 | */ |
||
183 | public function createScaledImages(SplFileInfo $sourceImage, ResponsiveImage $responsiveImage) : ResponsiveImage { |
||
208 | |||
209 | /** |
||
210 | * Optimize all sources of a Responsive Image |
||
211 | * |
||
212 | * @param ResponsiveImage $responsiveImage |
||
213 | * |
||
214 | * @return ResponsiveImage |
||
215 | */ |
||
216 | private function optimizeResponsiveImage(ResponsiveImage $responsiveImage) : ResponsiveImage { |
||
223 | |||
224 | /** |
||
225 | * @param string $directory |
||
226 | * @param string $path |
||
227 | * |
||
228 | * @return null|SplFileInfo |
||
229 | * @throws FileNotFoundException |
||
230 | */ |
||
231 | private function getImageFile(string $directory, string $path) { |
||
244 | |||
245 | /** |
||
246 | * @param string $driver |
||
247 | * |
||
248 | * @return ResponsiveFactory |
||
249 | */ |
||
250 | public function setDriver($driver) : ResponsiveFactory { |
||
255 | |||
256 | /** |
||
257 | * @param string $publicPath |
||
258 | * |
||
259 | * @return ResponsiveFactory |
||
260 | */ |
||
261 | public function setPublicPath($publicPath) : ResponsiveFactory { |
||
266 | |||
267 | /** |
||
268 | * @param boolean $enableCache |
||
269 | * |
||
270 | * @return ResponsiveFactory |
||
271 | */ |
||
272 | public function setEnableCache($enableCache) : ResponsiveFactory { |
||
277 | |||
278 | /** |
||
279 | * @param string $sourcePath |
||
280 | * |
||
281 | * @return ResponsiveFactory |
||
282 | */ |
||
283 | public function setSourcePath($sourcePath) : ResponsiveFactory { |
||
288 | |||
289 | /** |
||
290 | * @param Scaler $scaler |
||
291 | * |
||
292 | * @return ResponsiveFactory |
||
293 | */ |
||
294 | public function setScaler($scaler) : ResponsiveFactory { |
||
299 | |||
300 | /** |
||
301 | * @param bool $optimize |
||
302 | * |
||
303 | * @return ResponsiveFactory |
||
304 | */ |
||
305 | public function setOptimize(bool $optimize) : ResponsiveFactory { |
||
310 | |||
311 | /** |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getPublicPath() : string { |
||
317 | |||
318 | /** |
||
319 | * @param mixed $optimizerOptions |
||
320 | * |
||
321 | * @return ResponsiveFactory |
||
322 | */ |
||
323 | public function setOptimizerOptions($optimizerOptions) : ResponsiveFactory { |
||
328 | |||
329 | /** |
||
330 | * @param bool $rebase |
||
331 | * |
||
332 | * @return ResponsiveFactory |
||
333 | */ |
||
334 | public function setRebase(bool $rebase) : ResponsiveFactory { |
||
339 | |||
340 | } |
||
341 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: