forked from iConn/yii-easyimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EasyImage.php
367 lines (332 loc) · 11.9 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
<?php
/**
* EasyImage class file.
* @author Artur Zhdanov <[email protected]>
* @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 extends CApplicationComponent
{
/**
* Resizing directions
*/
const RESIZE_NONE = 0x01;
const RESIZE_WIDTH = 0x02;
const RESIZE_HEIGHT = 0x03;
const RESIZE_AUTO = 0x04;
const RESIZE_INVERSE = 0x05;
const RESIZE_PRECISE = 0x06;
/**
* Flipping directions
*/
const FLIP_HORIZONTAL = 0x11;
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 ($file) {
return $this->_image = Image::factory($this->detectPath($file), $driver ? $driver : $this->driver);
}
}
/**
* 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 {
$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);
}
/**
* 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())
{
// 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
$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;
}
/**
* 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->thumbSrcOf($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);
}
public function crop($width, $height, $offset_x = NULL, $offset_y = NULL)
{
return $this->image()->crop($width, $height, $offset_x, $offset_y);
}
public function rotate($degrees)
{
return $this->image()->rotate($degrees);
}
public function flip($direction)
{
return $this->image()->flip($direction);
}
public function sharpen($amount)
{
return $this->image()->sharpen($amount);
}
public function reflection($height = NULL, $opacity = 100, $fade_in = FALSE)
{
return $this->image()->reflection($height, $opacity, $fade_in);
}
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);
}
public function background($color, $opacity = 100)
{
return $this->image()->background($color, $opacity);
}
public function save($file = NULL, $quality = 100)
{
return $this->image()->save($file, $quality);
}
public function render($type = NULL, $quality = 100)
{
return $this->image()->render($type, $quality);
}
}