forked from iConn/yii-easyimage
/
EasyImage.php
510 lines (472 loc) · 15 KB
/
EasyImage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<?php
/**
* EasyImage class file.
* @author Artur Zhdanov <zhdanovartur@gmail.com>
* @copyright Copyright © Artur Zhdanov 2013-
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version 1.0.2
*/
Yii::setPathOfAlias('easyimage', dirname(__FILE__));
Yii::import('easyimage.drivers.*');
/**
* Class EasyImage
*/
class EasyImage extends CApplicationComponent
{
/**
* @var string
*/
public $password;
/**
* Resizing directions
*/
const RESIZE_NONE = 0x01;
/**
* @var
*/
const RESIZE_WIDTH = 0x02;
/**
* @var
*/
const RESIZE_HEIGHT = 0x03;
/**
* @var
*/
const RESIZE_AUTO = 0x04;
/**
* @var
*/
const RESIZE_INVERSE = 0x05;
/**
* @var
*/
const RESIZE_PRECISE = 0x06;
/**
* Flipping directions
*/
const FLIP_HORIZONTAL = 0x11;
/**
* @var
*/
const FLIP_VERTICAL = 0x12;
/**
* @var object Image
*/
private $_image;
/**
* @var string driver type: GD, Imagick
*/
public $driver = 'GD';
/**
* @var string relative path where the cache files are kept
*/
public $cachePath = '/assets/easyimage/';
/**
* @var int cache lifetime in seconds
*/
public $cacheTime = 2592000;
/**
* @var int value of quality: 0-100 (only for JPEG)
*/
public $quality = 100;
/**
* @var bool use retina-resolutions
* This setting increases the load on the server.
*/
public $retinaSupport = false;
/**
* Constructor.
*
* @param string $file
* @param string $driver
*/
public function __construct($file = null, $driver = null)
{
if (is_file($file)) {
return $this->_image = Image::factory($this->detectPath($file), $driver ? $driver : $this->driver);
}
return "";
}
/**
* Convert object to binary data of current image.
* Must be rendered with the appropriate Content-Type header or it will not be displayed correctly.
* @return string as binary
*/
public function __toString()
{
try {
return $this->image()->render();
} catch (CException $e) {
return '';
}
}
/**
*
*/
public function init()
{
// Publish "retina.js" library (http://retinajs.com/)
if ($this->retinaSupport) {
Yii::app()->clientScript->registerScriptFile(
Yii::app()->assetManager->publish(
Yii::getPathOfAlias('easyimage.assets') . '/retina.js'
),
CClientScript::POS_HEAD
);
}
}
/**
* This method returns the current Image instance.
* @return Image
* @throws CException
*/
public function image()
{
if ($this->_image instanceof Image) {
return $this->_image;
} else {
throw new CException('Don\'t have image');
}
}
/**
* This method detects which (absolute or relative) path is used.
*
* @param array $file path
*
* @return string path
*/
public function detectPath($file)
{
$fullPath = dirname(Yii::app()->basePath) . $file;
if (is_file($fullPath)) {
return $fullPath;
}
return $file;
}
/**
* Performance of image manipulation and save result.
*
* @param string $file the path to the original image
* @param string $newFile path to the resulting image
* @param array $params
*
* @return bool operation status
* @throws CException
*/
private function _doThumbOf($file, $newFile, $params)
{
if ($file instanceof Image) {
$this->_image = $file;
} else {
if (!is_file($file)) {
return false;
}
$this->_image = Image::factory($this->detectPath($file), $this->driver);
}
foreach ($params as $key => $value) {
switch ($key) {
case 'resize':
$this->resize(
isset($value['width']) ? $value['width'] : null,
isset($value['height']) ? $value['height'] : null,
isset($value['master']) ? $value['master'] : null
);
break;
case 'crop':
if (!isset($value['width']) || !isset($value['height'])) {
throw new CException('Params "width" and "height" is required for action "' . $key . '"');
}
$this->crop(
$value['width'],
$value['height'],
isset($value['offset_x']) ? $value['offset_x'] : null,
isset($value['offset_y']) ? $value['offset_y'] : null
);
break;
case 'rotate':
if (is_array($value)) {
if (!isset($value['degrees'])) {
throw new CException('Param "degrees" is required for action "' . $key . '"');
}
$this->rotate($value['degrees']);
} else {
$this->rotate($value);
}
break;
case 'flip':
if (is_array($value)) {
if (!isset($value['direction'])) {
throw new CException('Param "direction" is required for action "' . $key . '"');
}
$this->flip($value['direction']);
} else {
$this->flip($value);
}
break;
case 'sharpen':
if (is_array($value)) {
if (!isset($value['amount'])) {
throw new CException('Param "amount" is required for action "' . $key . '"');
}
$this->sharpen($value['amount']);
} else {
$this->sharpen($value);
}
break;
case 'reflection':
$this->reflection(
isset($value['height']) ? $value['height'] : null,
isset($value['opacity']) ? $value['opacity'] : 100,
isset($value['fade_in']) ? $value['fade_in'] : false
);
break;
case 'watermark':
if (is_array($value)) {
$this->watermark(
isset($value['watermark']) ? $value['watermark'] : null,
isset($value['offset_x']) ? $value['offset_x'] : null,
isset($value['offset_y']) ? $value['offset_y'] : null,
isset($value['opacity']) ? $value['opacity'] : 100
);
} else {
$this->watermark($value);
}
break;
case 'background':
if (is_array($value)) {
if (!isset($value['color'])) {
throw new CException('Param "color" is required for action "' . $key . '"');
}
$this->background(
$value['color'],
isset($value['opacity']) ? $value['opacity'] : 100
);
} else {
$this->background($value);
}
break;
case 'quality':
if (!isset($value)) {
throw new CException('Param "' . $key . '" can\'t be empty');
}
$this->quality = $value;
break;
case 'type':
break;
default:
throw new CException('Action "' . $key . '" is not found');
}
}
return $this->save($newFile, $this->quality);
}
/**
* @param $file
* @param array $params
*
* @return string
*/
public function getUrl($file, $params = array())
{
try {
$hash = md5($file . serialize($params));
$cachePath = Yii::getpathOfAlias('webroot') . $this->cachePath . $hash{0};
$cacheFileExt = isset($params['type']) ? $params['type'] : pathinfo($file, PATHINFO_EXTENSION);
$cacheFileName = $hash . '.' . $cacheFileExt;
$cacheFile = $cachePath . DIRECTORY_SEPARATOR . $cacheFileName;
$webCacheFile = Yii::app()->baseUrl . $this->cachePath . $hash{0} . '/' . $cacheFileName;
// Return URL to the cache image
if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $this->cacheTime)) {
return $webCacheFile;
} else {
$link = mcrypt_encrypt(MCRYPT_3DES, $this->password, serialize(array('f' => $file, 'p' => $params)), MCRYPT_MODE_ECB);
return Yii::app()->createAbsoluteUrl('user/image', array('params' => $link));
}
} catch (Exception $e) {
return '';
}
}
public function test($file)
{
return $this->thumbSrcOf($file, array(
'resize' => array(
'width' => 18,
'height' => 35
)
));
}
/**
* This method returns the URL to the cached thumbnail.
*
* @param string $file path
* @param array $params
*
* @return string URL path
*/
public function thumbSrcOf($file, $params = array())
{
try {
// Paths
$hash = md5($file . serialize($params));
$cachePath = Yii::getpathOfAlias('webroot') . $this->cachePath . $hash{0};
$cacheFileExt = isset($params['type']) ? $params['type'] : pathinfo($file, PATHINFO_EXTENSION);
$cacheFileName = $hash . '.' . $cacheFileExt;
$cacheFile = $cachePath . DIRECTORY_SEPARATOR . $cacheFileName;
$webCacheFile = Yii::app()->baseUrl . $this->cachePath . $hash{0} . '/' . $cacheFileName;
// Return URL to the cache image
if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $this->cacheTime)) {
return $webCacheFile;
}
// Make cache dir
if (!is_dir($cachePath)) {
@mkdir($cachePath, 0755, true);
}
// Create and caching thumbnail use params
if (!is_file($this->detectPath($file))) {
return false;
}
$image = Image::factory($this->detectPath($file), $this->driver);
$originWidth = $image->width;
$originHeight = $image->height;
$result = $this->_doThumbOf($image, $cacheFile, $params);
unset($image);
// Same for high-resolution image
if ($this->retinaSupport && $result) {
if ($this->image()->width * 2 <= $originWidth && $this->image()->height * 2 <= $originHeight) {
$retinaFile = $cachePath . DIRECTORY_SEPARATOR . $hash . '@2x.' . $cacheFileExt;
if (isset($params['resize']['width']) && isset($params['resize']['height'])) {
$params['resize']['width'] = $this->image()->width * 2;
$params['resize']['height'] = $this->image()->height * 2;
}
$this->_doThumbOf($file, $retinaFile, $params);
}
}
return $webCacheFile;
} catch (Exception $e) {
Yii::log($e->getMessage());
return '';
}
}
/**
* This method returns prepared HTML code for cached thumbnail.
* Use standard yii-component CHtml::image().
*
* @param string $file path
* @param array $params
* @param array $htmlOptions
*
* @return string HTML
*/
public function thumbOf($file, $params = array(), $htmlOptions = array())
{
return CHtml::image(
$this->getUrl($file, $params),
isset($htmlOptions['alt']) ? $htmlOptions['alt'] : '',
$htmlOptions
);
}
/**
* Description of the methods for the AutoComplete feature in a IDE
* because it uses a design pattern "factory".
*/
public function resize($width = null, $height = null, $master = null)
{
return $this->image()->resize($width, $height, $master);
}
/**
* @param $width
* @param $height
* @param null $offset_x
* @param null $offset_y
*
* @return $this
*/
public function crop($width, $height, $offset_x = null, $offset_y = null)
{
return $this->image()->crop($width, $height, $offset_x, $offset_y);
}
/**
* @param $degrees
*
* @return $this
*/
public function rotate($degrees)
{
return $this->image()->rotate($degrees);
}
/**
* @param $direction
*
* @return $this
*/
public function flip($direction)
{
return $this->image()->flip($direction);
}
/**
* @param $amount
*
* @return $this
*/
public function sharpen($amount)
{
return $this->image()->sharpen($amount);
}
/**
* @param null $height
* @param int $opacity
* @param bool $fade_in
*
* @return $this
*/
public function reflection($height = null, $opacity = 100, $fade_in = false)
{
return $this->image()->reflection($height, $opacity, $fade_in);
}
/**
* @param $watermark
* @param null $offset_x
* @param null $offset_y
* @param int $opacity
*
* @return $this
*/
public function watermark($watermark, $offset_x = null, $offset_y = null, $opacity = 100)
{
if ($watermark instanceof EasyImage) {
$watermark = $watermark->image();
} elseif (is_string($watermark)) {
$watermark = Image::factory(Yii::getpathOfAlias('webroot') . $watermark);
}
return $this->image()->watermark($watermark, $offset_x, $offset_y, $opacity);
}
/**
* @param $color
* @param int $opacity
*
* @return $this
*/
public function background($color, $opacity = 100)
{
return $this->image()->background($color, $opacity);
}
/**
* @param null $file
* @param int $quality
*
* @return bool
*/
public function save($file = null, $quality = 100)
{
return $this->image()->save($file, $quality);
}
/**
* @param null $type
* @param int $quality
*
* @return string
*/
public function render($type = null, $quality = 100)
{
return $this->image()->render($type, $quality);
}
}