1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Image; |
4
|
|
|
|
5
|
|
|
use Brendt\Image\Config\ResponsiveFactoryConfigurator; |
6
|
|
|
use Brendt\Image\Exception\FileNotFoundException; |
7
|
|
|
use Brendt\Image\Scaler\Scaler; |
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
|
|
|
/** |
17
|
|
|
* The image driver to use. |
18
|
|
|
* Available drivers: 'gd' and 'imagick'. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $driver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The source path to load images from. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $sourcePath; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The public path to save rendered images. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $publicPath; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Enabled cache will stop generated images from being overwritten. |
40
|
|
|
* |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $enableCache; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The Intervention image engine. |
47
|
|
|
* |
48
|
|
|
* @var ImageManager |
49
|
|
|
*/ |
50
|
|
|
protected $engine; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Filesystem |
54
|
|
|
*/ |
55
|
|
|
protected $fs; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Scaler |
59
|
|
|
*/ |
60
|
|
|
protected $scaler; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* ResponsiveFactory constructor. |
64
|
|
|
* |
65
|
|
|
* @param ResponsiveFactoryConfigurator $configurator |
66
|
|
|
*/ |
67
|
|
|
public function __construct(ResponsiveFactoryConfigurator $configurator) { |
68
|
|
|
$configurator->configure($this); |
69
|
|
|
$this->sourcePath = rtrim($this->sourcePath, '/'); |
70
|
|
|
$this->publicPath = rtrim($this->publicPath, '/'); |
71
|
|
|
$this->engine = new ImageManager([ |
72
|
|
|
'driver' => $this->driver, |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$this->fs = new Filesystem(); |
76
|
|
|
if (!$this->fs->exists($this->publicPath)) { |
77
|
|
|
$this->fs->mkdir($this->publicPath); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $src |
83
|
|
|
* |
84
|
|
|
* @return ResponsiveImage |
85
|
|
|
* @throws FileNotFoundException |
86
|
|
|
*/ |
87
|
|
|
public function create($src) { |
88
|
|
|
$responsiveImage = new ResponsiveImage($src); |
89
|
|
|
$src = $responsiveImage->src(); |
90
|
|
|
$sourceImage = $this->getImageFile($this->sourcePath, $src); |
91
|
|
|
|
92
|
|
|
if (!$sourceImage) { |
93
|
|
|
throw new FileNotFoundException("{$this->sourcePath}{$src}"); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$publicImagePath = "{$this->publicPath}/{$src}"; |
97
|
|
|
|
98
|
|
|
if (!$this->enableCache || !$this->fs->exists($publicImagePath)) { |
99
|
|
|
if ($this->fs->exists($publicImagePath)) { |
100
|
|
|
$this->fs->remove($publicImagePath); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->fs->dumpFile($publicImagePath, $sourceImage->getContents()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$extension = $sourceImage->getExtension(); |
107
|
|
|
$fileName = str_replace(".{$extension}", '', $sourceImage->getFilename()); |
108
|
|
|
|
109
|
|
|
$urlParts = explode('/', $src); |
110
|
|
|
array_pop($urlParts); |
111
|
|
|
$urlPath = implode('/', $urlParts); |
112
|
|
|
|
113
|
|
|
$responsiveImage->setExtension($extension); |
114
|
|
|
$responsiveImage->setFileName($fileName); |
115
|
|
|
$responsiveImage->setUrlPath($urlPath); |
116
|
|
|
|
117
|
|
|
$imageObject = $this->engine->make($sourceImage->getPathname()); |
118
|
|
|
$width = $imageObject->getWidth(); |
119
|
|
|
$responsiveImage->addSource($src, $width); |
120
|
|
|
|
121
|
|
|
$responsiveImage = $this->scaler->scale($responsiveImage, $imageObject); |
122
|
|
|
|
123
|
|
|
return $responsiveImage; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $directory |
128
|
|
|
* @param string $path |
129
|
|
|
* |
130
|
|
|
* @return SplFileInfo |
131
|
|
|
*/ |
132
|
|
|
private function getImageFile($directory, $path) { |
133
|
|
|
$iterator = Finder::create()->files()->in($directory)->path(ltrim($path, '/'))->getIterator(); |
134
|
|
|
$iterator->rewind(); |
135
|
|
|
|
136
|
|
|
return $iterator->current(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $driver |
141
|
|
|
* |
142
|
|
|
* @return ResponsiveFactory |
143
|
|
|
*/ |
144
|
|
|
public function setDriver($driver) { |
145
|
|
|
$this->driver = $driver; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $publicPath |
152
|
|
|
* |
153
|
|
|
* @return ResponsiveFactory |
154
|
|
|
*/ |
155
|
|
|
public function setPublicPath($publicPath) { |
156
|
|
|
$this->publicPath = $publicPath; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param boolean $enableCache |
163
|
|
|
* |
164
|
|
|
* @return ResponsiveFactory |
165
|
|
|
*/ |
166
|
|
|
public function setEnableCache($enableCache) { |
167
|
|
|
$this->enableCache = $enableCache; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $sourcePath |
174
|
|
|
* |
175
|
|
|
* @return ResponsiveFactory |
176
|
|
|
*/ |
177
|
|
|
public function setSourcePath($sourcePath) { |
178
|
|
|
$this->sourcePath = $sourcePath; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param Scaler $scaler |
185
|
|
|
*/ |
186
|
|
|
public function setScaler($scaler) { |
187
|
|
|
$this->scaler = $scaler; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
} |
191
|
|
|
|