-
Notifications
You must be signed in to change notification settings - Fork 1
/
Image.php
129 lines (111 loc) · 3.94 KB
/
Image.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
<?php
namespace andreosoft\image;
use yii\helpers\Url;
/**
* Description of Image
*
* @author Andrey Zagorets
*/
class Image {
/**
* Example of use:
*
*
* // generate a thumbnail image.
*
* <?php $options = [
*
* 'root' => \Yii::getAlias('@root'),
*
* 'webroot' => 'http://mysite.com/uploads',
*
* 'quality' => 50,
*
* 'cachedir' => \Yii::getAlias('@root'),
*
* 'resize' => 'h'|'w'|'max';
*
* 'watermark' =>
*
* ['file' => 'watermark.png',
*
* 'position' => 'topleft'|'topright'|'bottomleft'|'bottomright']
* ];
*
* echo Image::thumb('test-image.jpg', 120, 120);
* ?>
*
* @param $filename filename
* @param $width width
* @param $height height
* @param $options options
* @return url cache image.
*/
static function thumb($filename, $width = '', $height = '', $options = []) {
ini_set('memory_limit', '-1');
if (isset($options['root'])) {
$root = $options['root'];
} else {
$root = \Yii::getAlias('@uploads');
}
if (isset($options['cachedir'])) {
$cachedir = $options['cachedir'];
} else {
$cachedir = \Yii::getAlias('@uploads');
}
$empty = false;
if (!is_file($root . $filename)) {
if (isset($options['empty'])) {
$filename = '/' . $options['empty'];
} else {
$empty = true;
$filename = '/empty.jpg';
}
}
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$old_image = $filename;
$new_image = '/cache/' . md5(substr($filename, 0, strrpos($filename, '.')) . '-' . $width . 'x' . $height . '-' . json_encode($options)) . '.' . $extension;
if (!is_file($cachedir . $new_image) || (filectime($root . $old_image) > filectime($cachedir . $new_image))) {
$path = '';
$directories = explode('/', dirname(str_replace('../', '', $new_image)));
foreach ($directories as $directory) {
$path = $path . '/' . $directory;
if (!is_dir($cachedir . $path)) {
@mkdir($cachedir . $path, 0775);
}
}
list($width_orig, $height_orig) = getimagesize($root . $old_image);
if ($width_orig != $width || $height_orig != $height || !empty($width) || !empty($height)) {
$image = new CoreImage($root . $old_image);
if (isset($options['resize'])) {
$image->resize($width, $height, $options['resize']);
} else {
$image->resize($width, $height);
}
if (isset($options['watermark']) && !$empty) {
$position = 'bottomleft';
if (isset($options['watermark']['position'])) {
$position = $options['watermark']['position'];
}
$percent = 0.3;
if (isset($options['watermark']['percent'])) {
$percent = $options['watermark']['percent'];
}
$image->watermark($root . $options['watermark']['file'], $position, $percent);
}
if (isset($options['quality'])) {
$image->save($cachedir . $new_image, $options['quality']);
} else {
$image->save($cachedir . $new_image);
}
} else {
copy($root . $old_image, $cachedir . $new_image);
}
}
if (isset($options['webroot'])) {
return Url::to($options['webroot'] . $new_image);
} else {
return Url::to('@webuploads' . $new_image);
}
}
}