forked from CostaRico/yii2-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
executable file
·189 lines (149 loc) · 4.51 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
<?php
namespace rico\yii2images;
use rico\yii2images\models\PlaceHolder;
use yii;
use rico\yii2images\models\Image;
class Module extends \yii\base\Module
{
public $imagesStorePath = '@app/web/store';
public $imagesCachePath = '@app/web/imgCache';
public $graphicsLibrary = 'GD';
public $controllerNamespace = 'rico\yii2images\controllers';
public $placeHolderPath;
public $waterMark = false;
public $className;
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
if(empty($this->className)) {
$imageQuery = Image::find();
} else {
$class = $this->className;
$imageQuery = $class::find();
}
$image = $imageQuery
->where([
'modelName' => $modelName,
'itemId' => $itemId,
'urlAlias' => $alias
])
/* ->where('modelName = :modelName AND itemId = :itemId AND urlAlias = :alias',
[
':modelName' => $modelName,
':itemId' => $itemId,
':alias' => $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 = \yii\helpers\Inflector::pluralize($modelName).'/'. $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];
}
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!!!');
// custom initialization code goes here
}
public function getPlaceHolder(){
if($this->placeHolderPath){
return new PlaceHolder();
}else{
return null;
}
}
}