|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\image\models; |
|
4
|
|
|
|
|
5
|
|
|
use app\behaviors\ImageExist; |
|
6
|
|
|
use Imagine\Image\Box; |
|
7
|
|
|
use Imagine\Image\ImageInterface; |
|
8
|
|
|
use League\Flysystem\Filesystem; |
|
9
|
|
|
use League\Flysystem\Util; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\helpers\ArrayHelper; |
|
12
|
|
|
use yii\imagine\Image as Imagine; |
|
13
|
|
|
use yii\web\BadRequestHttpException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This is the model class for table "{{%thumbnail}}". |
|
17
|
|
|
* @property integer $id |
|
18
|
|
|
* @property integer $img_id |
|
19
|
|
|
* @property string $thumb_path |
|
20
|
|
|
* @property integer $size_id |
|
21
|
|
|
*/ |
|
22
|
|
|
class Thumbnail extends \yii\db\ActiveRecord |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @inheritdoc |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function tableName() |
|
28
|
|
|
{ |
|
29
|
|
|
return '{{%thumbnail}}'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @inheritdoc |
|
34
|
|
|
*/ |
|
35
|
|
|
public function rules() |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
[['img_id', 'thumb_path', 'size_id'], 'required'], |
|
39
|
|
|
[['img_id', 'size_id'], 'integer'], |
|
40
|
|
|
[['thumb_path'], 'string', 'max' => 255] |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function behaviors() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
[ |
|
48
|
|
|
'class' => ImageExist::className(), |
|
49
|
|
|
'srcAttrName' => 'thumb_path', |
|
50
|
|
|
] |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function attributeLabels() |
|
58
|
|
|
{ |
|
59
|
|
|
return [ |
|
60
|
|
|
'id' => Yii::t('app', 'ID'), |
|
61
|
|
|
'img_id' => Yii::t('app', 'Img ID'), |
|
62
|
|
|
'thumb_path' => Yii::t('app', 'Thumb Src'), |
|
63
|
|
|
'size_id' => Yii::t('app', 'Size ID'), |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Return thumb of image by size or create if not exist |
|
69
|
|
|
* @param $image Image |
|
70
|
|
|
* @param $size ThumbnailSize |
|
71
|
|
|
* @return Thumbnail |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getImageThumbnailBySize($image, $size) |
|
74
|
|
|
{ |
|
75
|
|
|
$cacheKey = 'thumbBySize:'.$image->id . ';'.$size->id; |
|
76
|
|
|
$thumb = Yii::$app->cache->get($cacheKey); |
|
77
|
|
|
if ($thumb === false) { |
|
78
|
|
|
$thumb = static::findOne(['img_id' => $image->id, 'size_id' => $size->id]); |
|
79
|
|
|
if ($thumb === null) { |
|
80
|
|
|
$thumb = new Thumbnail; |
|
81
|
|
|
$thumb->setAttributes( |
|
82
|
|
|
[ |
|
83
|
|
|
'img_id' => $image->id, |
|
84
|
|
|
'size_id' => $size->id, |
|
85
|
|
|
] |
|
86
|
|
|
); |
|
87
|
|
|
$thumb->thumb_path = static::createThumbnail($image, $size); |
|
88
|
|
|
$thumb->save(); |
|
89
|
|
|
} |
|
90
|
|
|
Yii::$app->cache->set($cacheKey, $thumb, 86400); |
|
91
|
|
|
} |
|
92
|
|
|
return $thumb; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Create thumbnail in fs |
|
97
|
|
|
* @param $image Image |
|
98
|
|
|
* @param $size ThumbnailSize |
|
99
|
|
|
* @return string|false |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function createThumbnail($image, $size) |
|
102
|
|
|
{ |
|
103
|
|
|
try { |
|
104
|
|
|
/** @var Filesystem $fs */ |
|
105
|
|
|
$fs = Yii::$app->getModule('image')->fsComponent; |
|
106
|
|
|
|
|
107
|
|
|
$file = Imagine::getImagine()->read($fs->readStream($image->filename)); |
|
108
|
|
|
/** @var ImageInterface $thumb */ |
|
109
|
|
|
if($size->resize_mode == ThumbnailSize::RESIZE){ |
|
110
|
|
|
$thumb = $file->resize(new Box($size->width, $size->height)); |
|
111
|
|
|
}else{ |
|
112
|
|
|
$thumb = $file->thumbnail(new Box($size->width, $size->height), $size->resize_mode); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$path = Yii::$app->getModule('image')->thumbnailsDirectory; |
|
116
|
|
|
|
|
117
|
|
|
if (!preg_match('#^(?<name>.+)\.(?<ext>[^\.]+)$#', $image->filename, $fileInfo)) { |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$stream = $thumb->get($fileInfo['ext'], ['quality' => $size->quality]); |
|
122
|
|
|
$src = "$path/{$fileInfo['name']}-{$size->width}x{$size->height}.{$fileInfo['ext']}"; |
|
123
|
|
|
$fs->put($src, $stream); |
|
124
|
|
|
return $src; |
|
125
|
|
|
} catch (\Exception $e) { |
|
126
|
|
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @inheritdoc |
|
132
|
|
|
* @param bool $insert |
|
133
|
|
|
* @param array $changedAttributes |
|
134
|
|
|
* @throws BadRequestHttpException |
|
135
|
|
|
*/ |
|
136
|
|
|
public function afterSave($insert, $changedAttributes) |
|
137
|
|
|
{ |
|
138
|
|
|
parent::afterSave($insert, $changedAttributes); |
|
139
|
|
|
if (Yii::$app->getModule('image')->useWatermark == 1) { |
|
140
|
|
|
/** @var ThumbnailSize $size */ |
|
141
|
|
|
$size = ThumbnailSize::findOne(ArrayHelper::getValue($this, 'size_id', 0)); |
|
142
|
|
|
if ($size !== null) { |
|
143
|
|
|
$watermark = Watermark::findOne($size->default_watermark_id); |
|
144
|
|
|
if ($watermark !== null) { |
|
145
|
|
|
ThumbnailWatermark::getThumbnailWatermark($this, $watermark); |
|
146
|
|
|
} |
|
147
|
|
|
} else { |
|
148
|
|
|
throw new BadRequestHttpException(Yii::t('app', 'Set thumbnail size')); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @inheritdoc |
|
155
|
|
|
* @throws \Exception |
|
156
|
|
|
*/ |
|
157
|
|
View Code Duplication |
public function afterDelete() |
|
158
|
|
|
{ |
|
159
|
|
|
parent::afterDelete(); |
|
160
|
|
|
$sameImages = static::findAll(['thumb_path' => $this->thumb_path]); |
|
161
|
|
|
if (empty($sameImages) === true) { |
|
162
|
|
|
if (Yii::$app->getModule('image')->fsComponent->has($this->thumb_path)) { |
|
163
|
|
|
Yii::$app->getModule('image')->fsComponent->delete($this->thumb_path); |
|
164
|
|
|
} |
|
165
|
|
|
$thumbnailWatermarks = ThumbnailWatermark::findAll(['thumb_id' => $this->id]); |
|
166
|
|
|
foreach ($thumbnailWatermarks as $thumbnailWatermark) { |
|
167
|
|
|
$thumbnailWatermark->delete(); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|