1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Framework\Http\Controllers; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Cache\Item; |
19
|
|
|
use O2System\Framework\Http\Controller; |
20
|
|
|
use O2System\Image\Manipulation; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Images |
24
|
|
|
* |
25
|
|
|
* @package O2System\Framework\Http\Controllers |
26
|
|
|
*/ |
27
|
|
|
class Images extends Controller |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Images::$inherited |
31
|
|
|
* |
32
|
|
|
* Controller inherited flag. |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
static public $inherited = true; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Images::$storagePath |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $storagePath; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Images::$imageNotFoundFilename |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
public $imageNotFoundFilename = 'not-found.jpg'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Images::$imageFilePath |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
public $imageFilePath = null; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Images::$imageFileMime |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
public $imageFileMime = null; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Images::$imageSize |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
public $imageSize = [ |
72
|
|
|
'width' => null, |
73
|
|
|
'height' => null, |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Images::$imageScale |
78
|
|
|
* |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
public $imageScale = null; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Images::$imageQuality |
85
|
|
|
* |
86
|
|
|
* @var int |
87
|
|
|
*/ |
88
|
|
|
public $imageQuality = null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Images::$imageCrop |
92
|
|
|
* |
93
|
|
|
* @var bool |
94
|
|
|
*/ |
95
|
|
|
public $imageCrop = false; |
96
|
|
|
|
97
|
|
|
// ------------------------------------------------------------------------ |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Images::__construct |
101
|
|
|
*/ |
102
|
|
|
public function __construct() |
103
|
|
|
{ |
104
|
|
|
$this->storagePath = PATH_STORAGE . 'images' . DIRECTORY_SEPARATOR; |
105
|
|
|
$this->imageNotFoundFilename = $this->storagePath . 'not-found.jpg'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// ------------------------------------------------------------------------ |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Images::route |
112
|
|
|
*/ |
113
|
|
|
public function route() |
114
|
|
|
{ |
115
|
|
|
$segments = server_request()->getUri()->segments->getArrayCopy(); |
116
|
|
|
|
117
|
|
|
if (false !== ($key = array_search('images', $segments))) { |
118
|
|
|
$segments = array_slice($segments, $key); |
|
|
|
|
119
|
|
|
} else { |
120
|
|
|
array_shift($segments); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->imageFilePath = $this->imageNotFoundFilename; |
124
|
|
|
|
125
|
|
|
$this->imageSize[ 'width' ] = $this->input->get('width'); |
|
|
|
|
126
|
|
|
$this->imageSize[ 'height' ] = $this->input->get('height'); |
127
|
|
|
$this->imageScale = $this->input->get('scale'); |
128
|
|
|
$this->imageQuality = $this->input->get('quality'); |
129
|
|
|
$this->imageCrop = $this->input->get('crop'); |
130
|
|
|
|
131
|
|
|
if (false !== ($key = array_search('crop', $segments))) { |
132
|
|
|
$this->imageCrop = true; |
133
|
|
|
unset($segments[ $key ]); |
134
|
|
|
$segments = array_values($segments); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (count($segments) == 1) { |
138
|
|
|
$this->imageFilePath = $this->storagePath . end($segments); |
139
|
|
|
} elseif (count($segments) >= 2) { |
140
|
|
|
if (preg_match("/(\d+)(x)(\d+)/", $segments[ count($segments) - 2 ], $matches)) { |
141
|
|
|
$this->imageSize[ 'width' ] = $matches[ 1 ]; |
142
|
|
|
$this->imageSize[ 'height' ] = $matches[ 3 ]; |
143
|
|
|
|
144
|
|
|
if (count($segments) == 2) { |
145
|
|
|
$this->imageFilePath = $this->storagePath . end($segments); |
146
|
|
|
} else { |
147
|
|
|
$this->imageFilePath = $this->storagePath . implode(DIRECTORY_SEPARATOR, |
148
|
|
|
array_slice($segments, 0, |
149
|
|
|
count($segments) - 2)) . DIRECTORY_SEPARATOR . end($segments); |
150
|
|
|
} |
151
|
|
|
} elseif (preg_match("/(\d+)(p)/", $segments[ count($segments) - 2 ], |
152
|
|
|
$matches) or is_numeric($segments[ count($segments) - 2 ]) |
153
|
|
|
) { |
154
|
|
|
$this->imageScale = isset($matches[ 1 ]) ? $matches[ 1 ] : $segments[ count($segments) - 2 ]; |
155
|
|
|
if (count($segments) == 2) { |
156
|
|
|
$this->imageFilePath = $this->storagePath . end($segments); |
157
|
|
|
} else { |
158
|
|
|
$this->imageFilePath = $this->storagePath . implode(DIRECTORY_SEPARATOR, |
159
|
|
|
array_slice($segments, 0, |
160
|
|
|
count($segments) - 2)) . DIRECTORY_SEPARATOR . end($segments); |
161
|
|
|
} |
162
|
|
|
} else { |
163
|
|
|
$this->imageFilePath = $this->storagePath . implode(DIRECTORY_SEPARATOR, $segments); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$imageFilePath = $this->imageFilePath; |
168
|
|
|
$extensions[ 0 ] = pathinfo($imageFilePath, PATHINFO_EXTENSION); |
|
|
|
|
169
|
|
|
|
170
|
|
|
for ($i = 0; $i < 2; $i++) { |
171
|
|
|
$extension = pathinfo($imageFilePath, PATHINFO_EXTENSION); |
172
|
|
|
|
173
|
|
|
if ($extension !== '') { |
174
|
|
|
$extensions[ $i ] = $extension; |
175
|
|
|
$imageFilePath = str_replace('.' . $extensions[ $i ], '', $imageFilePath); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$mimes = [ |
180
|
|
|
'gif' => 'image/gif', |
181
|
|
|
'jpg' => 'image/jpeg', |
182
|
|
|
'png' => 'image/png', |
183
|
|
|
'webp' => 'image/webp', |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
if (count($extensions) == 2) { |
187
|
|
|
$this->imageFilePath = $imageFilePath . '.' . $extensions[ 1 ]; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (array_key_exists($extension = reset($extensions), $mimes)) { |
191
|
|
|
$this->imageFileMime = $mimes[ $extension ]; |
192
|
|
|
} elseif (array_key_exists($extension = pathinfo($this->imageFilePath, PATHINFO_EXTENSION), $mimes)) { |
193
|
|
|
$this->imageFileMime = $mimes[ $extension ]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if ( ! is_file($this->imageFilePath)) { |
197
|
|
|
$this->imageFilePath = $this->imageNotFoundFilename; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if ( ! empty($this->imageScale)) { |
201
|
|
|
$this->scale(); |
202
|
|
|
} elseif ( ! empty($this->imageSize[ 'width' ]) || ! empty($this->imageSize[ 'height' ])) { |
203
|
|
|
$this->resize(); |
204
|
|
|
} else { |
205
|
|
|
$this->original(); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// ------------------------------------------------------------------------ |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Images::scale |
213
|
|
|
* |
214
|
|
|
* @throws \O2System\Spl\Exceptions\Runtime\FileNotFoundException |
215
|
|
|
*/ |
216
|
|
|
protected function scale() |
217
|
|
|
{ |
218
|
|
|
$config = config('image', true); |
219
|
|
|
|
220
|
|
|
if ( ! empty($this->imageQuality)) { |
221
|
|
|
$config->offsetSet('quality', intval($this->imageQuality)); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if ($config->cached === true) { |
|
|
|
|
225
|
|
|
if ($this->imageFilePath !== $this->imageNotFoundFilename) { |
226
|
|
|
$cacheImageKey = 'image-' . $this->imageScale . '-' . str_replace($this->storagePath, '', |
227
|
|
|
$this->imageFilePath); |
228
|
|
|
|
229
|
|
|
if (cache()->hasItemPool('images')) { |
230
|
|
|
$cacheItemPool = cache()->getItemPool('images'); |
231
|
|
|
|
232
|
|
|
if ($cacheItemPool->hasItem($cacheImageKey)) { |
233
|
|
|
$cacheImageString = $cacheItemPool->getItem($cacheImageKey)->get(); |
234
|
|
|
|
235
|
|
|
$manipulation = new Manipulation($config); |
|
|
|
|
236
|
|
|
$manipulation->setImageFile($this->imageFilePath); |
237
|
|
|
$manipulation->setImageString($cacheImageString); |
238
|
|
|
$manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime); |
239
|
|
|
} else { |
240
|
|
|
$manipulation = new Manipulation($config); |
241
|
|
|
$manipulation->setImageFile($this->imageFilePath); |
242
|
|
|
$manipulation->scaleImage($this->imageScale); |
|
|
|
|
243
|
|
|
$cacheItemPool->save(new Item($cacheImageKey, $manipulation->getBlobImage(), false)); |
|
|
|
|
244
|
|
|
|
245
|
|
|
$manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime); |
246
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$manipulation = new Manipulation($config); |
253
|
|
|
$manipulation->setImageFile($this->imageFilePath); |
254
|
|
|
$manipulation->scaleImage($this->imageScale); |
255
|
|
|
$manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime); |
256
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
// ------------------------------------------------------------------------ |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Images::resize |
263
|
|
|
* |
264
|
|
|
* @throws \O2System\Spl\Exceptions\Runtime\FileNotFoundException |
265
|
|
|
*/ |
266
|
|
|
protected function resize() |
267
|
|
|
{ |
268
|
|
|
$config = config('image', true); |
269
|
|
|
|
270
|
|
|
if ( ! empty($this->imageQuality)) { |
271
|
|
|
$config->offsetSet('quality', intval($this->imageQuality)); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
if ($config->cached === true) { |
|
|
|
|
275
|
|
|
if ($this->imageFilePath !== $this->imageNotFoundFilename) { |
276
|
|
|
$cacheImageKey = 'image-' . ($this->input->get('crop') ? 'crop-' : '') . implode('x', |
|
|
|
|
277
|
|
|
$this->imageSize) . '-' . str_replace($this->storagePath, '', $this->imageFilePath); |
278
|
|
|
|
279
|
|
|
if (cache()->hasItemPool('images')) { |
280
|
|
|
$cacheItemPool = cache()->getItemPool('images'); |
281
|
|
|
|
282
|
|
|
if ($cacheItemPool->hasItem($cacheImageKey)) { |
283
|
|
|
$cacheImageString = $cacheItemPool->getItem($cacheImageKey)->get(); |
284
|
|
|
|
285
|
|
|
$manipulation = new Manipulation($config); |
|
|
|
|
286
|
|
|
$manipulation->setImageFile($this->imageFilePath); |
287
|
|
|
$manipulation->setImageString($cacheImageString); |
288
|
|
|
$manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime); |
289
|
|
|
} else { |
290
|
|
|
$manipulation = new Manipulation($config); |
291
|
|
|
$manipulation->setImageFile($this->imageFilePath); |
292
|
|
|
$manipulation->resizeImage($this->imageSize[ 'width' ], $this->imageSize[ 'height' ], |
293
|
|
|
(bool)$this->imageCrop); |
294
|
|
|
$cacheItemPool->save(new Item($cacheImageKey, $manipulation->getBlobImage(), false)); |
|
|
|
|
295
|
|
|
|
296
|
|
|
$manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime); |
297
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$manipulation = new Manipulation($config); |
304
|
|
|
$manipulation->setImageFile($this->imageFilePath); |
305
|
|
|
$manipulation->resizeImage($this->imageSize[ 'width' ], $this->imageSize[ 'height' ], (bool)$this->imageCrop); |
306
|
|
|
$manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime); |
307
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
// ------------------------------------------------------------------------ |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Images::original |
314
|
|
|
* |
315
|
|
|
* @throws \O2System\Spl\Exceptions\Runtime\FileNotFoundException |
316
|
|
|
*/ |
317
|
|
|
protected function original() |
318
|
|
|
{ |
319
|
|
|
$config = config('image', true); |
320
|
|
|
|
321
|
|
|
if ( ! empty($this->imageQuality)) { |
322
|
|
|
$config->offsetSet('quality', intval($this->imageQuality)); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
$manipulation = new Manipulation($config); |
|
|
|
|
326
|
|
|
$manipulation->setImageFile($this->imageFilePath); |
327
|
|
|
$manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime); |
328
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
329
|
|
|
} |
330
|
|
|
} |