ImageManager.php
5.67 KB
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
<?php
namespace artbox\core\components\imagemanager\models;
use artbox\core\components\imagemanager\Module;
use Yii;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
use yii\behaviors\BlameableBehavior;
/**
* This is the model class for table "ImageManager".
*
* @property integer $id
* @property string $fileName
* @property string $fileHash
* @property string $created
* @property string $modified
* @property string $createdBy
* @property string $modifiedBy
*/
class ImageManager extends ActiveRecord
{
/**
* Set Created date to now
*/
public function behaviors()
{
$aBehaviors = [];
// Add the time stamp behavior
$aBehaviors[] = [
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created',
'updatedAtAttribute' => 'modified',
'value' => new Expression('NOW()'),
];
// Check if we need blamable behavior
if (Yii::$app->getModule('imagemanager')->setBlameableBehavior) {
$aBehaviors[] = [
'class' => BlameableBehavior::className(),
'createdByAttribute' => 'createdBy',
'updatedByAttribute' => 'modifiedBy',
];
}
return $aBehaviors;
}
/**
* @inheritdoc
*/
public static function tableName()
{
return 'ImageManager';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[
[
'fileName',
'fileHash',
],
'required',
],
[
[
'created',
'modified',
],
'safe',
],
[
[ 'fileName' ],
'string',
'max' => 128,
],
[
[ 'fileHash' ],
'string',
'max' => 32,
],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('imagemanager', 'ID'),
'fileName' => Yii::t('imagemanager', 'File Name'),
'fileHash' => Yii::t('imagemanager', 'File Hash'),
'created' => Yii::t('imagemanager', 'Created'),
'modified' => Yii::t('imagemanager', 'Modified'),
'createdBy' => Yii::t('imagemanager', 'Created by'),
'modifiedBy' => Yii::t('imagemanager', 'Modified by'),
];
}
/**
* Get image path private
*
* @return string|null If image file exists the path to the image, if file does not exists null
*/
public function getImagePathPrivate()
{
//set default return
$return = null;
//set media path
$sMediaPath = \Yii::$app->imagemanager->mediaPath;
$sFileExtension = pathinfo($this->fileName, PATHINFO_EXTENSION);
//get image file path
$sImageFilePath = $sMediaPath . '/' . $this->id . '_' . $this->fileHash . '.' . $sFileExtension;
//check file exists
if (file_exists($sImageFilePath)) {
$return = $sImageFilePath;
}
return $return;
}
/**
* Get image data dimension/size
*
* @return array The image sizes
*/
public function getImageDetails()
{
//set default return
$return = [
'width' => 0,
'height' => 0,
'size' => 0,
];
//set media path
$sMediaPath = \Yii::$app->imagemanager->mediaPath;
$sFileExtension = pathinfo($this->fileName, PATHINFO_EXTENSION);
//get image file path
$sImageFilePath = $sMediaPath . '/' . $this->id . '_' . $this->fileHash . '.' . $sFileExtension;
//check file exists
if (file_exists($sImageFilePath)) {
$aImageDimension = getimagesize($sImageFilePath);
$return[ 'width' ] = isset($aImageDimension[ 0 ]) ? $aImageDimension[ 0 ] : 0;
$return[ 'height' ] = isset($aImageDimension[ 1 ]) ? $aImageDimension[ 1 ] : 0;
$return[ 'size' ] = Yii::$app->formatter->asShortSize(filesize($sImageFilePath), 2);
}
return $return;
}
/**
* @return ActiveQuery
*/
public function getLanguages()
{
return ImageManagerLang::find()
->where(
[
'image_id' => $this->id,
]
)
->joinWith('language')
->where(
[
'language.status' => true,
]
);
}
}