1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Code from CambioCMS project |
5
|
|
|
* @link http://code.google.com/p/cambiocms/source/browse/trunk/cms/includes/602_html.image.php |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
class Ajde_Resource_Image extends Ajde_Resource |
9
|
|
|
{ |
10
|
|
|
protected $_cache; |
11
|
|
|
protected $_source; |
12
|
|
|
protected $_type; |
13
|
|
|
protected $_image; |
14
|
|
|
|
15
|
|
|
public static $_thumbDir = '.thumbnails'; |
16
|
|
|
|
17
|
|
|
const ORIENTATION_PORTRAIT = 'portrait'; |
18
|
|
|
const ORIENTATION_LANDSCAPE = 'landscape'; |
19
|
|
|
|
20
|
|
|
public function __construct($file) |
21
|
|
|
{ |
22
|
|
|
if (file_exists(LOCAL_ROOT.$file)) { |
23
|
|
|
$this->_source = $file; |
24
|
|
|
} else { |
25
|
|
|
$this->_source = MEDIA_DIR.'notfound.png'; |
26
|
|
|
} |
27
|
|
|
$this->_type = $this->extension(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getUrl($width = null, $height = null, $crop = false) |
31
|
|
|
{ |
32
|
|
|
$this->setWidth($width); |
|
|
|
|
33
|
|
|
$this->setHeight($height); |
|
|
|
|
34
|
|
|
$this->setCrop($crop); |
|
|
|
|
35
|
|
|
|
36
|
|
|
return $this->getLinkUrl(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getLinkUrl() |
40
|
|
|
{ |
41
|
|
|
// Double url encoding because of mod_rewrite url decoding bug |
42
|
|
|
// @see http://www.php.net/manual/en/reserved.variables.php#84025 |
43
|
|
|
$url = '_core/component:image/'.urlencode(urlencode($this->getFingerprint())).'.data'; |
44
|
|
|
|
45
|
|
|
if (config('app.debug') === true) { |
46
|
|
|
$url .= '?file='.str_replace(['%2F', '%5C'], ':', urlencode($this->_source)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $url; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function fromFingerprint($fingerprint) |
53
|
|
|
{ |
54
|
|
|
$array = self::decodeFingerprint($fingerprint); |
55
|
|
|
extract($array); |
56
|
|
|
$image = new self($s); |
57
|
|
|
$image->setWidth($w); |
|
|
|
|
58
|
|
|
$image->setHeight($h); |
|
|
|
|
59
|
|
|
$image->setCrop($c); |
|
|
|
|
60
|
|
|
|
61
|
|
|
return $image; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getFingerprint() |
65
|
|
|
{ |
66
|
|
|
$w = $this->hasWidth() ? $this->getWidth(false) : null; |
|
|
|
|
67
|
|
|
$h = $this->hasHeight() ? $this->getHeight(false) : null; |
|
|
|
|
68
|
|
|
$array = ['s' => $this->_source, 'w' => $w, 'h' => $h, 'c' => $this->getCrop()]; |
|
|
|
|
69
|
|
|
|
70
|
|
|
return $this->encodeFingerprint($array); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getContents() |
74
|
|
|
{ |
75
|
|
|
return $this->getImage(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function getImage() |
79
|
|
|
{ |
80
|
|
|
if (isset($this->_cache)) { |
81
|
|
|
$image = file_get_contents($this->_cache); |
82
|
|
|
} else { |
83
|
|
|
ob_start(); |
84
|
|
|
switch ($this->_type) { |
85
|
|
|
case 'jpg': |
86
|
|
|
case 'jpeg': |
87
|
|
|
imagejpeg($this->getImageResource()); |
88
|
|
|
break; |
89
|
|
|
case 'png': |
90
|
|
|
imagepng($this->getImageResource()); |
91
|
|
|
break; |
92
|
|
|
case 'gif': |
93
|
|
|
imagegif($this->getImageResource()); |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
$image = ob_get_contents(); |
97
|
|
|
ob_end_clean(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $image; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function __sleep() |
104
|
|
|
{ |
105
|
|
|
$this->_image = null; |
106
|
|
|
|
107
|
|
|
return ['_source', '_type', '_data']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function __wakeup() |
111
|
|
|
{ |
112
|
|
|
//$this->_image = $this->getImageResource(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getImageResource() |
116
|
|
|
{ |
117
|
|
|
// try to allocate a lot of memory |
118
|
|
|
ini_set('memory_limit', '150M'); |
119
|
|
|
|
120
|
|
|
if (!isset($this->_image)) { |
121
|
|
|
switch ($this->_type) { |
122
|
|
|
case 'jpg': |
123
|
|
|
case 'jpeg': |
124
|
|
|
$this->_image = imagecreatefromjpeg(LOCAL_ROOT.$this->_source); |
125
|
|
|
break; |
126
|
|
|
case 'png': |
127
|
|
|
$this->_image = imagecreatefrompng(LOCAL_ROOT.$this->_source); |
128
|
|
|
break; |
129
|
|
|
case 'gif': |
130
|
|
|
$this->_image = imagecreatefromgif(LOCAL_ROOT.$this->_source); |
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->_image; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getCalculatedDim() |
139
|
|
|
{ |
140
|
|
|
return [ |
141
|
|
|
'width' => imagesx($this->getImageResource()), |
142
|
|
|
'height' => imagesy($this->getImageResource()), |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function imageInCache($width, $height, $crop = true) |
147
|
|
|
{ |
148
|
|
|
if (is_file($cache = LOCAL_ROOT.$this->getGeneratedFilename($width, $height, $crop))) { |
149
|
|
|
$this->_cache = $cache; |
150
|
|
|
|
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function saveCached($width, $height, $crop = true) |
158
|
|
|
{ |
159
|
|
|
$this->save($this->getGeneratedFilename($width, $height, $crop)); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getOriginalFilename() |
163
|
|
|
{ |
164
|
|
|
return $this->_source; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getFilename() |
168
|
|
|
{ |
169
|
|
|
return $this->getGeneratedFilename(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getGeneratedFilename($width = null, $height = null, $crop = null) |
173
|
|
|
{ |
174
|
|
|
if ( |
175
|
|
|
!isset($width) && !$this->hasWidth() && |
|
|
|
|
176
|
|
|
!isset($height) && !$this->hasHeight() && |
|
|
|
|
177
|
|
|
!isset($crop) && !$this->hasCrop() |
|
|
|
|
178
|
|
|
) { |
179
|
|
|
return $this->_source; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (!isset($width) && $this->hasWidth()) { |
|
|
|
|
183
|
|
|
$width = $this->getWidth(); |
184
|
|
|
} |
185
|
|
|
if (!isset($height) && $this->hasHeight()) { |
|
|
|
|
186
|
|
|
$height = $this->getHeight(); |
187
|
|
|
} |
188
|
|
|
if (!isset($crop) && $this->hasCrop()) { |
|
|
|
|
189
|
|
|
$crop = $this->getCrop(); |
|
|
|
|
190
|
|
|
} elseif (!isset($crop)) { |
191
|
|
|
$crop = true; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// see if we have a thumb folder |
195
|
|
|
$thumbPath = $this->dir().'/'.self::$_thumbDir; |
196
|
|
|
if (!is_dir($thumbPath)) { |
197
|
|
|
@mkdir($thumbPath); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$filename = $this->dir(); |
201
|
|
|
$filename .= '/'.self::$_thumbDir.'/'; |
202
|
|
|
$filename .= $this->filename(); |
203
|
|
|
$filename .= '_'.$width.'x'.$height; |
204
|
|
|
$filename .= ($crop ? 'c' : ''); |
205
|
|
|
$filename .= '.'; |
206
|
|
|
$filename .= $this->extension(); |
207
|
|
|
|
208
|
|
|
return $filename; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getOrientation() |
212
|
|
|
{ |
213
|
|
|
return ($this->getHeight() >= $this->getWidth()) ? self::ORIENTATION_PORTRAIT : self::ORIENTATION_LANDSCAPE; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
View Code Duplication |
public function getHeight($calculate = true) |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
if ($this->has('height') && !$this->isEmpty('height')) { |
219
|
|
|
return $this->get('height'); |
220
|
|
|
} else { |
221
|
|
|
if ($calculate === true) { |
222
|
|
|
$old_y = imagesy($this->getImageResource()); |
223
|
|
|
if ($this->has('width') && !$this->isEmpty('width')) { |
224
|
|
|
$old_x = imagesx($this->getImageResource()); |
225
|
|
|
|
226
|
|
|
return (int) (($old_y / $old_x) * $this->get('width')); |
227
|
|
|
} else { |
228
|
|
|
return $old_y; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return 0; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
View Code Duplication |
public function getWidth($calculate = true) |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
if ($this->has('width') && !$this->isEmpty('width')) { |
239
|
|
|
return $this->get('width'); |
240
|
|
|
} else { |
241
|
|
|
if ($calculate === true) { |
242
|
|
|
$old_x = imagesx($this->getImageResource()); |
243
|
|
|
if ($this->has('height') && !$this->isEmpty('height')) { |
244
|
|
|
$old_y = imagesy($this->getImageResource()); |
245
|
|
|
|
246
|
|
|
return (int) (($old_x / $old_y) * $this->get('height')); |
247
|
|
|
} else { |
248
|
|
|
return $old_x; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return 0; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function resize($height, $width, $crop = true, $xCorrection = 0, $yCorrection = 0) |
257
|
|
|
{ |
258
|
|
|
if ($this->imageInCache($width, $height, $crop)) { |
259
|
|
|
return; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$oldWidth = imagesx($this->getImageResource()); |
263
|
|
|
$oldHeight = imagesy($this->getImageResource()); |
264
|
|
|
|
265
|
|
|
if (empty($height)) { |
266
|
|
|
$height = (int) (($oldHeight / $oldWidth) * $width); |
267
|
|
|
} |
268
|
|
|
if (empty($width)) { |
269
|
|
|
$width = (int) (($oldWidth / $oldHeight) * $height); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if ($this->imageInCache($width, $height, $crop)) { |
273
|
|
|
return; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$newImage = imagecreatetruecolor($width, $height); |
277
|
|
|
|
278
|
|
|
$newWidth = $width; |
279
|
|
|
$newHeight = intval($oldHeight * ($width / $oldWidth)); |
280
|
|
|
|
281
|
|
|
$x_offset = 0; |
282
|
|
|
$y_offset = 0; |
283
|
|
|
|
284
|
|
|
$adjustWidth = (($crop === true) ? ($newHeight < $height) : ($newHeight > $height)); |
285
|
|
|
$offsetSign = -1; |
286
|
|
|
|
287
|
|
|
if ($adjustWidth) { |
288
|
|
|
$newHeight = $height; |
289
|
|
|
$newWidth = intval($oldWidth * ($height / $oldHeight)); |
290
|
|
|
|
291
|
|
|
// Correct for cropping left / right |
292
|
|
|
$x_offset = $offsetSign * intval(($newWidth - $width) / 2); |
293
|
|
|
} else { |
294
|
|
|
// Correct for cropping top / bottom |
295
|
|
|
$y_offset = $offsetSign * intval(($newHeight - $height) / 2); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$x_offset = $x_offset + $xCorrection; |
299
|
|
|
$y_offset = $y_offset + $yCorrection; |
300
|
|
|
|
301
|
|
|
$this->fastimagecopyresampled($newImage, $this->getImageResource(), $x_offset, $y_offset, 0, 0, $newWidth, |
302
|
|
|
$newHeight, $oldWidth, $oldHeight, 5); |
303
|
|
|
|
304
|
|
|
$this->_image = $newImage; |
305
|
|
|
|
306
|
|
|
$this->saveCached($width, $height, $crop); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function crop($height, $width) |
310
|
|
|
{ |
311
|
|
|
return $this->resize($height, $width, true); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function save($target) |
315
|
|
|
{ |
316
|
|
|
switch ($this->_type) { |
317
|
|
|
case 'jpg': |
318
|
|
|
case 'jpeg': |
319
|
|
|
imagejpeg($this->_image, LOCAL_ROOT.$target); |
320
|
|
|
break; |
321
|
|
|
case 'png': |
322
|
|
|
imagepng($this->_image, LOCAL_ROOT.$target); |
323
|
|
|
break; |
324
|
|
|
case 'gif': |
325
|
|
|
imagegif($this->_image, LOCAL_ROOT.$target); |
326
|
|
|
break; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
public function getMimeType() |
331
|
|
|
{ |
332
|
|
|
switch ($this->_type) { |
333
|
|
|
case 'jpg': |
334
|
|
|
case 'jpeg': |
335
|
|
|
return 'image/jpeg'; |
336
|
|
|
break; |
|
|
|
|
337
|
|
|
case 'png': |
338
|
|
|
return 'image/png'; |
339
|
|
|
break; |
|
|
|
|
340
|
|
|
case 'gif': |
341
|
|
|
return 'image/gif'; |
342
|
|
|
break; |
|
|
|
|
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
public function getBase64() |
347
|
|
|
{ |
348
|
|
|
return 'data:'.$this->getMimeType().';base64,'.base64_encode($this->getImage()); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function destroy() |
352
|
|
|
{ |
353
|
|
|
imagedestroy($this->_image); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
protected function extension() |
357
|
|
|
{ |
358
|
|
|
$path_info = pathinfo($this->_source); |
359
|
|
|
|
360
|
|
|
return strtolower(issetor($path_info['extension'])); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
protected function dir() |
364
|
|
|
{ |
365
|
|
|
$path_info = pathinfo($this->_source); |
366
|
|
|
|
367
|
|
|
return $path_info['dirname']; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
protected function filename() |
371
|
|
|
{ |
372
|
|
|
$path_info = pathinfo($this->_source); |
373
|
|
|
|
374
|
|
|
return $path_info['filename']; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
protected function fastimagecopyresampled( |
378
|
|
|
&$dst_image, |
379
|
|
|
$src_image, |
380
|
|
|
$dst_x, |
381
|
|
|
$dst_y, |
382
|
|
|
$src_x, |
383
|
|
|
$src_y, |
384
|
|
|
$dst_w, |
385
|
|
|
$dst_h, |
386
|
|
|
$src_w, |
387
|
|
|
$src_h, |
388
|
|
|
$quality = 4 |
389
|
|
|
) { |
390
|
|
|
// Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled. |
391
|
|
|
// Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled". |
392
|
|
|
// Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting. |
393
|
|
|
// Author: Tim Eckel - Date: 09/07/07 - Version: 1.1 - Project: FreeRingers.net - Freely distributable - These comments must remain. |
394
|
|
|
// |
395
|
|
|
// Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5. Must be greater than zero. |
396
|
|
|
// Between 0 and 1 = Fast, but mosaic results, closer to 0 increases the mosaic effect. |
397
|
|
|
// 1 = Up to 350 times faster. Poor results, looks very similar to imagecopyresized. |
398
|
|
|
// 2 = Up to 95 times faster. Images appear a little sharp, some prefer this over a quality of 3. |
399
|
|
|
// 3 = Up to 60 times faster. Will give high quality smooth results very close to imagecopyresampled, just faster. |
400
|
|
|
// 4 = Up to 25 times faster. Almost identical to imagecopyresampled for most images. |
401
|
|
|
// 5 = No speedup. Just uses imagecopyresampled, no advantage over imagecopyresampled. |
402
|
|
|
|
403
|
|
|
// work to do? |
404
|
|
|
if (empty($src_image) || empty($dst_image) || $quality <= 0) { |
405
|
|
|
return false; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
// This is the resizing/resampling/transparency-preserving magic |
409
|
|
|
// https://github.com/maxim/smart_resize_image/blob/master/smart_resize_image.function.php |
410
|
|
|
if ($this->_type == 'png' || $this->_type == 'gif') { |
411
|
|
|
$transparency = imagecolortransparent($src_image); |
412
|
|
|
if ($transparency >= 0) { |
413
|
|
|
$transparent_color = imagecolorsforindex($src_image, $trnprt_indx); |
|
|
|
|
414
|
|
|
$transparency = imagecolorallocate($dst_image, $trnprt_color['red'], $trnprt_color['green'], |
|
|
|
|
415
|
|
|
$trnprt_color['blue']); |
416
|
|
|
imagefill($dst_image, 0, 0, $transparency); |
417
|
|
|
imagecolortransparent($dst_image, $transparency); |
418
|
|
|
} elseif ($this->_type == 'png') { |
419
|
|
|
imagealphablending($dst_image, false); |
420
|
|
|
$color = imagecolorallocatealpha($dst_image, 0, 0, 0, 127); |
421
|
|
|
imagefill($dst_image, 0, 0, $color); |
422
|
|
|
imagesavealpha($dst_image, true); |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
// do the resize |
427
|
|
|
if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) { |
428
|
|
|
$temp = imagecreatetruecolor($dst_w * $quality + 1, $dst_h * $quality + 1); |
429
|
|
|
imagecopyresized($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, |
430
|
|
|
$src_w, $src_h); |
431
|
|
|
imagecopyresampled($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, |
432
|
|
|
$dst_h * $quality); |
433
|
|
|
imagedestroy($temp); |
434
|
|
|
} else { |
435
|
|
|
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
return true; |
439
|
|
|
} |
440
|
|
|
} |
441
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: