-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
executable file
·290 lines (226 loc) · 7.91 KB
/
Module.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
<?php
namespace rico\yii2images;
use rico\yii2images\models\PlaceHolderGD;
use rico\yii2images\models\PlaceHolderImagick;
use rico\yii2images\models\UrlManager;
use SebastianBergmann\Exporter\Exception;
use yii;
use yii\helpers\Url;
class Module extends \yii\base\Module
{
public $imageClasses = [
'GD' => 'rico\yii2images\models\ImageGD',
'Imagick' => 'rico\yii2images\models\ImageImagick'
];
public $waterMarkClasses = [
'GD' => 'rico\yii2images\effects\WaterMarkGD',
'Imagick' => 'rico\yii2images\effects\WaterMarkImagick'
];
const IMAGE_BASE_CLASS = 'rico\yii2images\models\ImageAbstract';
const IMAGE_INTERFACE_CLASS = 'rico\yii2images\models\ImageInterface';
public $placeHolderClass = 'rico\yii2images\models\PlaceHolder';
public $imagesStorePath = '@app/web/store';
public $imagesCachePath = '@app/web/imgCache';
public $graphicsLibrary = 'GD';
public $controllerNamespace = 'rico\yii2images\controllers';
public $effectsFactoryInterfaceClass = 'rico\yii2images\inters\EffectsFactoryInterface';
public $effectInterfaceClasses = null;
const GD_ID = 'GD';
const IMAGICK_ID = 'Imagick';
public $placeHolderPath;
public $waterMark = false;
public $urlManager = null;
public $removeImageUrl = null;
public $setMainImageUrl = null;
public $effects = [];
public $urlPrefix = 'yii2images';
public $urlRules = [
'<alias>.<extension>' => 'images/image-by-alias'
];
public function init()
{
parent::init();
if (!$this->imagesStorePath
or
!$this->imagesCachePath
or
$this->imagesStorePath == '@app'
or
$this->imagesCachePath == '@app'
)
throw new \Exception('Setup imagesStorePath and imagesCachePath images module properties!!!');
$this->urlManager = new UrlManager();
$this->registerEffects();
$this->removeImageUrl = Url::toRoute([
'/' . $this->id . '/images/remove-image'
]);
$this->setMainImageUrl = Url::toRoute([
'/' . $this->id . '/images/set-main-image'
]);
$this->effects['waterMark'] = $this->waterMarkClasses[$this->graphicsLibrary];
$this->effectInterfaceClasses = [
self::GD_ID => 'rico\yii2images\inters\GDEffectInterface',
self::IMAGICK_ID => 'rico\yii2images\inters\ImagickEffectInterface'
];
}
public function imageClass()
{
$imgClassName = null;
if (!isset($this->imageClasses[$this->graphicsLibrary])) {
throw new yii\base\Exception('I cant find correct Image class for your graphics library, config array must contain "GD" or "imagick" key.');
} else {
$imgClassName = $this->imageClasses[$this->graphicsLibrary];
}
if (!is_subclass_of($imgClassName, self::IMAGE_BASE_CLASS)) {
throw new yii\base\Exception('Image class must be child of Image Abstract class ' . self::IMAGE_BASE_CLASS);
}
if (!is_subclass_of($imgClassName, self::IMAGE_INTERFACE_CLASS)) {
throw new yii\base\Exception('Image class must implements interface ' . self::IMAGE_INTERFACE_CLASS);
}
return $imgClassName;
}
public function getImage($item, $dirtyAlias)
{
//Get params
$params = $data = $this->parseImageAlias($dirtyAlias);
$alias = $params['alias'];
$size = $params['size'];
$itemId = preg_replace('/[^0-9]+/', '', $item);
$modelName = preg_replace('/[0-9]+/', '', $item);
//Lets get image
$imageClass = $this->imageClass();
$image = $imageClass::find()
->where([
'modelName' => $modelName,
'itemId' => $itemId,
'urlAlias' => $alias
])
->one();
if (!$image) {
return $this->getPlaceHolder();
}
return $image;
}
public function getStorePath()
{
return Yii::getAlias($this->imagesStorePath);
}
public function getCachePath()
{
return Yii::getAlias($this->imagesCachePath);
}
public function getModelSubDir($model)
{
$modelName = $this->getShortClass($model);
$modelDir = $modelName . 's/' . $modelName . $model->id;
return $modelDir;
}
public function getShortClass($obj)
{
$className = get_class($obj);
if (preg_match('@\\\\([\w]+)$@', $className, $matches)) {
$className = $matches[1];
}
return $className;
}
/**
*
* Parses size string
* For instance: 400x400, 400x, x400
*
* @param $notParsedSize
* @return array|null
*/
public function parseSize($notParsedSize)
{
$sizeParts = explode('x', $notParsedSize);
$part1 = (isset($sizeParts[0]) and $sizeParts[0] != '');
$part2 = (isset($sizeParts[1]) and $sizeParts[1] != '');
if ($part1 && $part2) {
if (intval($sizeParts[0]) > 0
&&
intval($sizeParts[1]) > 0
) {
$size = [
'width' => intval($sizeParts[0]),
'height' => intval($sizeParts[1])
];
} else {
$size = null;
}
} elseif ($part1 && !$part2) {
$size = [
'width' => intval($sizeParts[0]),
'height' => null
];
} elseif (!$part1 && $part2) {
$size = [
'width' => null,
'height' => intval($sizeParts[1])
];
} else {
throw new \Exception('Something bad with size, sorry!');
}
return $size;
}
public function parseImageAlias($parameterized)
{
$params = explode('_', $parameterized);
if (count($params) == 1) {
$alias = $params[0];
$size = null;
} elseif (count($params) == 2) {
$alias = $params[0];
$size = $this->parseSize($params[1]);
if (!$size) {
$alias = null;
}
} else {
$alias = null;
$size = null;
}
return ['alias' => $alias, 'size' => $size];
}
private function registerEffects()
{
foreach ($this->effects as $effect) {
if($this->checkEffect($effect)){
$this->effects[$effect::getCode()] = $effect;
}
}
}
public function checkEffect($effectClassName)
{
if (class_implements($effectClassName, $this->effectInterfaceClasses[self::IMAGICK_ID])) {
if ($this->graphicsLibrary != self::IMAGICK_ID) {
throw new \Exception('Effect class must implement Imagick Effect interface');
}
} elseif (class_implements($effectClassName, $this->effectInterfaceClasses[self::GD_ID])) {
if ($this->graphicsLibrary != self::GD_ID) {
throw new \Exception('Effect class must implement GD Effect interface');
}
} else {
throw new \Exception('Effect class must implement one of effect interfaces');
}
return true;
}
public function getEffect($effectId)
{
if (!isset($this->effects[$effectId])) {
throw new Exception('Cant find effect ' . print_r($effectId, true));
}
return $this->effects[$effectId];
}
public function getPlaceHolder()
{
if ($this->placeHolderPath) {
if(!file_exists(Yii::getAlias($this->placeHolderPath))){
throw new \Exception('PlaceHolder property defined, but placeholder file nor exists!!!');
}
$placeHolderClass = 'rico\yii2images\models\PlaceHolder'.$this->graphicsLibrary;
return new $placeHolderClass;
} else {
return null;
}
}
}