/
ImagesModule.php
163 lines (140 loc) · 3.82 KB
/
ImagesModule.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
<?php
/**
* Author: Kulikov Roman
* Email: flinnraider@yandex.ru
*/
namespace romkaChev\yii2\images;
use romkaChev\yii2\images\models\Image;
use romkaChev\yii2\images\models\Placeholder;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\base\Module;
use yii\helpers\Inflector;
/**
* Class ImagesModule
* @package romkaChev\yii2\images
*
* @property Placeholder placeholder
*/
class ImagesModule extends Module {
public $ds = DIRECTORY_SEPARATOR;
public $storePath;
public $publishPath;
public $publishUrl;
public $placeholderPath;
public $watermarkPath;
public $useWatermark = false;
public $imageClass = '\romkaChev\yii2\images\models\Image';
public $placeholderClass = '\romkaChev\yii2\images\models\Placeholder';
/**
* @inheritdoc
* @throws \yii\base\InvalidConfigException
*/
public function init() {
parent::init();
if ( $this->storePath === null ) {
throw new InvalidConfigException( 'StorePath property must be set' );
}
if ( $this->publishPath === null ) {
throw new InvalidConfigException( 'PublishPath property must be set' );
}
if ( $this->publishUrl === null ) {
throw new InvalidConfigException( 'PublishUrl property must be set' );
}
if ( $this->placeholderPath !== null ) {
if ( ! is_file( \Yii::getAlias( $this->placeholderPath ) ) ) {
throw new \RuntimeException( "Placeholder at path '{$this->placeholderPath}' was not found" );
}
}
}
/**
* @param \yii\base\Model $model
*
* @return string
*/
public function getModelDirectory( Model $model ) {
if ( ! $model->canGetProperty( 'idAttribute' ) ) {
throw new \LogicException( "Model {$model->className()} has not 'idAttribute' property" );
}
$modelName = $this->getShortClass( $model );
/** @noinspection PhpUndefinedFieldInspection */
$modelId = $model->{$model->idAttribute};
return $this->_getSubDirectory( $modelName, $modelId );
}
/**
* @param mixed $object
*
* @return string
*/
public function getShortClass( $object ) {
$className = get_class( $object );
if ( preg_match( '@\\\\([\w]+)$@', $className, $matches ) ) {
$className = $matches[1];
}
return $className;
}
/**
* @param string $modelName
* @param int $modelId
*
* @return string
*/
protected function _getSubDirectory( $modelName, $modelId ) {
$modelDir = "{$modelName}{$modelId}";
$baseDir = Inflector::pluralize( $modelName );
return "{$baseDir}{$this->ds}{$modelDir}";
}
/**
* @param \romkaChev\yii2\images\models\Image $image
*
* @return string
*/
public function getImageDirectory( Image $image ) {
return $this->_getSubDirectory( $image->modelName, $image->modelId );
}
/**
* @param string $stringSize
*
* @return int[]
*/
public function parseSize( $stringSize ) {
$sizeParts = explode( 'x', $stringSize );
$isPart1 = ( isset( $sizeParts[0] ) && $sizeParts[0] != '' );
$isPart2 = ( isset( $sizeParts[1] ) && $sizeParts[1] != '' );
if ( $isPart1 && $isPart2 ) {
if ( intval( $sizeParts[0] ) > 0
&&
intval( $sizeParts[1] ) > 0
) {
$size = [
'width' => intval( $sizeParts[0] ),
'height' => intval( $sizeParts[1] )
];
} else {
$size = null;
}
} elseif ( $isPart1 && ! $isPart2 ) {
$size = [
'width' => intval( $sizeParts[0] ),
'height' => null
];
} elseif ( ! $isPart1 && $isPart2 ) {
$size = [
'width' => null,
'height' => intval( $sizeParts[1] )
];
} else {
throw new \LogicException( "Something bad with size '{$stringSize}'" );
}
return $size;
}
/**
* @return \romkaChev\yii2\images\models\Placeholder
*/
public function getPlaceholder() {
if ( $this->placeholderPath === null ) {
throw new \LogicException( "Trying to get placeholder, but 'placeholderPath' property was not set." );
}
return new $this->placeholderClass( $this->placeholderPath );
}
}