-
Notifications
You must be signed in to change notification settings - Fork 12
/
ImageCache.php
271 lines (233 loc) · 7.99 KB
/
ImageCache.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
<?php
namespace iutbay\yii2imagecache;
use Yii;
use yii\imagine\Image;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use Imagine\Image\Color;
use Imagine\Image\ManipulatorInterface;
use Imagine\Image\Point;
/**
* ImageCache Component
* @author Kevin LEVRON <[email protected]>
*/
class ImageCache extends \yii\base\Component
{
const SIZE_THUMB = 'thumb';
const SIZE_MEDIUM = 'medium';
const SIZE_LARGE = 'large';
const SIZE_FULL = 'full';
public $sourcePath;
public $sourceUrl;
public $thumbsPath;
public $thumbsUrl;
public $resizeMode;
public $sizes = [
self::SIZE_THUMB => [150, 150],
self::SIZE_MEDIUM => [300, 300],
self::SIZE_LARGE => [600, 600],
];
public $defaultSizeSuffix = '_';
public $sizeSuffixes = [];
//public $checkPathRegexp = '#[/-a-z0-9_\.]*#i';
public $extensions = [
'jpg' => 'jpeg',
'jpeg' => 'jpeg',
'png' => 'png',
'gif' => 'gif',
'bmp' => 'bmp',
];
public $text;
//public $watermark;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (!isset($this->sourcePath) || !isset($this->sourceUrl))
throw new \yii\base\InvalidConfigException('Invalid sourcePath/sourceUrl.');
if (!isset($this->thumbsPath) || !isset($this->thumbsUrl)) {
$this->thumbsPath = '@app/web/thumbs';
$this->thumbsUrl = '@web/thumbs';
}
if (isset($this->text)) {
if (!isset($this->text['text']) || !isset($this->text['fontFile']))
throw new \yii\base\InvalidConfigException('Invalid text.');
}
$this->sourcePath = Yii::getAlias($this->sourcePath);
$this->sourceUrl = Yii::getAlias($this->sourceUrl);
$this->thumbsPath = Yii::getAlias($this->thumbsPath);
$this->thumbsUrl = Yii::getAlias($this->thumbsUrl);
$this->sourcePath = str_replace('\\', '/', $this->sourcePath);
$this->thumbsPath = str_replace('\\', '/', $this->thumbsPath);
if (!isset($this->resizeMode))
$this->resizeMode = ManipulatorInterface::THUMBNAIL_OUTBOUND;
}
/**
* Get thumb img tag
* @param string $path
* @param string $size
* @return string
*/
public function thumb($path, $size = self::SIZE_THUMB, $imgOptions = [])
{
return Html::img(self::thumbSrc($path, $size), $imgOptions);
}
/**
* Get thumb src
* @param string $path
* @param string $size
* @return string
*/
public function thumbSrc($path, $size = self::SIZE_THUMB)
{
$path = Yii::getAlias($path);
if ($size != self::SIZE_FULL && !isset($this->sizes[$size]))
throw new \yii\base\InvalidParamException('Unkown size ' . $size);
$realPath = str_replace($this->sourceUrl, $this->sourcePath, $path);
if (!file_exists($realPath) || !preg_match('#^(.*)\.(' . $this->getExtensionsRegexp() . ')$#', $path, $matches))
throw new \yii\base\InvalidParamException('Invalid path ' . $path);
$suffix = $this->getSufixFromSize($size);
$src = "{$matches[1]}{$suffix}.{$matches[2]}";
$src = str_replace($this->sourceUrl, $this->thumbsUrl, $src);
$src = str_replace('%', '%25', $src);
return $src;
}
/**
* Create thumb
* @param string $path thumb path
* @param boolean $overwrite
* @return boolean
*/
public function create($path, $overwrite = true)
{
// test path
$info = $this->getPathInfo($path);
if (!is_array($info))
return false;
// check original image
if (!file_exists($info['srcPath']))
return false;
// check destination folder
$folder = preg_replace('#/[^/]*$#', '', $info['dstPath']);
if (!file_exists($folder))
@mkdir($folder, 0777, true);
// create thumb
return $this->createThumb($info['srcPath'], $info['dstPath'], $info['size'], $this->resizeMode);
}
/**
* Output thumb to browser
* @param string $path thumb path
*/
public function output($path)
{
// test path
$info = $this->getPathInfo($path);
if (!is_array($info) || (!file_exists($info['dstPath']) && !$this->create($path)))
return false;
// send image to browser
header('Content-type: image/' . $this->extensions[$info['extension']]);
header('Content-Length: ' . filesize($info['dstPath']));
readfile($info['dstPath']);
exit();
}
/**
* Create thumb
* @param string $srcPath
* @param string $dstPath
* @param string $size
* @param string $mode
* @return boolean
*/
public function createThumb($srcPath, $dstPath, $size, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND)
{
if ($size == self::SIZE_FULL) {
$thumb = Image::getImagine()->open($srcPath);
} else {
$width = $this->sizes[$size][0];
$height = $this->sizes[$size][1];
$thumb = Image::thumbnail($srcPath, $width, $height, $mode);
}
if (isset($this->text)) {
$fontOptions = ArrayHelper::getValue($this->text, 'fontOptions', []);
$fontSize = ArrayHelper::getValue($fontOptions, 'size', 12);
$fontColor = ArrayHelper::getValue($fontOptions, 'color', 'fff');
$fontAngle = ArrayHelper::getValue($fontOptions, 'angle', 0);
$start = ArrayHelper::getValue($this->text, 'start', [0, 0]);
$palette = new \Imagine\Image\Palette\RGB();
$font = Image::getImagine()->font(Yii::getAlias($this->text['fontFile']), $fontSize, $palette->color($fontColor));
$thumb->draw()->text($this->text['text'], $font, new Point($start[0], $start[1]), $fontAngle);
}
if ($thumb && $thumb->save($dstPath))
return true;
return false;
}
/**
* Get info from path
* @param string $path
* @return null|array
*/
private function getPathInfo($path)
{
$regexp = '#^(.*)(' . $this->getSizeSuffixesRegexp() . ')\.(' . $this->getExtensionsRegexp() . ')$#';
if (preg_match($regexp, $path, $matches)) {
return [
'size' => $this->getSizeFromSuffix($matches[2]),
'srcPath' => $this->sourcePath . '/' . $matches[1] . '.' . $matches[3],
'dstPath' => $this->thumbsPath . '/' . $path,
'extension' => $matches[3],
];
} else if (preg_match('#^(.*)\.(' . $this->getExtensionsRegexp() . ')$#', $path, $matches)) {
return [
'size' => self::SIZE_FULL,
'srcPath' => $this->sourcePath . '/' . $matches[1] . '.' . $matches[2],
'dstPath' => $this->thumbsPath . '/' . $path,
'extension' => $matches[2],
];
}
}
/**
* Get size suffixes regexp
* @return string regexp
*/
private function getSizeSuffixesRegexp()
{
return join('|', $this->getSizeSuffixes());
}
/**
* Get extensions regexp
* @return string regexp
*/
public function getExtensionsRegexp()
{
$keys = array_keys($this->extensions);
return '(?i)' . join('|', $keys);
}
/**
* Get size from suffix
* @param string $suffix
* @return string size
*/
private function getSizeFromSuffix($suffix)
{
return array_search($suffix, $this->getSizeSuffixes());
}
/**
* Get suffix from size
* @param string $size
* @return string suffix
*/
private function getSufixFromSize($size)
{
return ArrayHelper::getValue($this->getSizeSuffixes(), $size);
}
private function getSizeSuffixes()
{
$suffixes = [];
foreach ($this->sizes as $size => $sizeConf) {
$suffixes[$size] = ArrayHelper::getValue($this->sizeSuffixes, $size, $this->defaultSizeSuffix . $size);
}
return $suffixes;
}
}