-
Notifications
You must be signed in to change notification settings - Fork 5
/
ThumbBehavior.php
82 lines (71 loc) · 2.42 KB
/
ThumbBehavior.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
<?php
namespace developeruz\behaviors;
/**
* ThumbBehavior for Yii2
*
* @author Elle <elleuz@gmail.com>
* @version 0.1
* @package Behaviors for Yii2
*
*/
use Yii;
use yii\behaviors\AttributeBehavior;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;
use yii\imagine\Image;
class ThumbBehavior extends AttributeBehavior
{
public $fileAttribute = 'image';
public $saveDir = '/../frontend/web/upload/';
public $previewSize = [[50, 50], [150, 150]];
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_VALIDATE => 'addImages',
ActiveRecord::EVENT_BEFORE_DELETE => 'deleteImages'
];
}
public function addImages()
{
$image = UploadedFile::getInstance($this->owner, $this->fileAttribute);
if (!empty($image)) {
$uniqName = Yii::$app->security->generateRandomString() . '.' . $image->getExtension();
if ($image->saveAs(Yii::$app->basePath . $this->saveDir . $uniqName)) {
$this->owner->{$this->fileAttribute} = $uniqName;
$this->makePreview($uniqName);
}
}
}
public function deleteImages()
{
if (file_exists(Yii::$app->basePath . $this->saveDir . $this->owner->{$this->fileAttribute})) {
unlink(Yii::$app->basePath . $this->saveDir . $this->owner->{$this->fileAttribute});
}
$this->removePreview($this->owner->{$this->fileAttribute});
}
public function getImage()
{
return $this->owner->{$this->fileAttribute};
}
public function getPreview($size)
{
if(file_exists(Yii::$app->basePath.$this->saveDir.$size[0].'x'.$size[1].$this->owner->{$this->fileAttribute}))
return $size[0].'x'.$size[1].$this->owner->{$this->fileAttribute};
else return false;
}
protected function makePreview($file)
{
foreach ($this->previewSize as $size) {
Image::thumbnail(Yii::$app->basePath . $this->saveDir . $file, $size[0], $size[1])
->save(Yii::$app->basePath . $this->saveDir . $size[0] . 'x' . $size[1] . $file);
}
}
protected function removePreview($file)
{
foreach ($this->previewSize as $size) {
if (file_exists(Yii::$app->basePath . $this->saveDir . $size[0] . 'x' . $size[1] . $file)) {
unlink(Yii::$app->basePath . $this->saveDir . $size[0] . 'x' . $size[1] . $file);
}
}
}
}