UploadForm::delete()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 7
c 1
b 0
f 1
dl 0
loc 12
rs 9.6111
cc 5
nc 3
nop 1
1
<?php
2
3
namespace modules\users\models;
4
5
use Yii;
6
use yii\base\Exception;
7
use yii\base\Model;
8
use yii\web\UploadedFile;
9
use yii\helpers\FileHelper;
10
use yii\imagine\Image;
11
use Imagine\Image\ImageInterface;
12
use modules\users\Module;
13
14
/**
15
 * Class UploadForm
16
 * @package modules\users\models
17
 */
18
class UploadForm extends Model
19
{
20
    const PREFIX_ORIGINAL = 'original_';
21
    const PREFIX_THUMBNAIL = 'thumb_';
22
    const EXTENSIONS = 'png, jpg';
23
24
    /**
25
     * @var UploadedFile
26
     */
27
    public $imageFile;
28
    /**
29
     * Upload directory
30
     * @var string
31
     */
32
    public $directory = '@frontend/runtime/avatars';
33
    /**
34
     * File Name
35
     * @var string
36
     */
37
    public $fileName = 'avatar';
38
    /**
39
     * File extension
40
     * @var string
41
     */
42
    public $fileExtension = 'jpg';
43
    /**
44
     * @var null|int
45
     */
46
    public $width = 540;
47
    /**
48
     * @var null|int
49
     */
50
    public $height;
51
    /**
52
     * @var int
53
     */
54
    public $avatarWidth = 150;
55
    /**
56
     * @var int
57
     */
58
    public $avatarHeight = 150;
59
    /**
60
     * @var int
61
     */
62
    public $avatarQuality = 100;
63
    /**
64
     * @var int
65
     */
66
    public $cropWidth;
67
    /**
68
     * @var int
69
     */
70
    public $cropHeight;
71
    /**
72
     * @var int
73
     */
74
    public $cropX;
75
    /**
76
     * @var int
77
     */
78
    public $cropY;
79
    /**
80
     * @var int
81
     */
82
    public $quality = 80;
83
84
    /**
85
     * @var int
86
     */
87
    public $cropQuality = 100;
88
89
    /**
90
     * Upload path
91
     * @var string|bool
92
     */
93
    private $path = '';
94
95
    /**
96
     * @inheritDoc
97
     */
98
    public function init()
99
    {
100
        parent::init();
101
        $this->path = $this->path ?: $this->getPath(Yii::$app->user->id);
102
    }
103
104
    /**
105
     * Rules
106
     * @inheritDoc
107
     * @return array|array[]
108
     */
109
    public function rules()
110
    {
111
        return [
112
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => self::EXTENSIONS],
113
            [['cropWidth', 'cropHeight', 'cropX', 'cropY'], 'integer']
114
        ];
115
    }
116
117
    /**
118
     * Labels
119
     * @return array
120
     */
121
    public function attributeLabels()
122
    {
123
        return [
124
            'imageFile' => Module::translate('module', 'Image'),
125
            'cropWidth' => Module::translate('module', 'Crop Width'),
126
            'cropHeight' => Module::translate('module', 'Crop Height'),
127
            'cropX' => Module::translate('module', 'Crop X'),
128
            'cropY' => Module::translate('module', 'Crop Y'),
129
        ];
130
    }
131
132
    /**
133
     * Upload
134
     * @return array|string
135
     * @throws Exception
136
     */
137
    public function upload()
138
    {
139
        if ($this->validate()) {
140
            $this->createDirectory();
141
142
            $fileName = $this->getFileName();
143
            $path = $this->path . DIRECTORY_SEPARATOR . self::PREFIX_ORIGINAL . $fileName;
144
            if ($this->imageFile->saveAs($path)) {
145
                $this->resize();
146
                $this->delete($path);
147
                return $this->path . DIRECTORY_SEPARATOR . self::PREFIX_THUMBNAIL . $fileName;
148
            }
149
        }
150
        return $this->errors;
151
    }
152
153
    /**
154
     * File Name
155
     * @return string
156
     */
157
    public function getFileName()
158
    {
159
        return $this->fileName . '.' . $this->fileExtension;
160
    }
161
162
    /**
163
     * Resize
164
     * @param $width int
165
     * @param $height int
166
     * @return ImageInterface
167
     */
168
    public function resize($width = null, $height = null)
169
    {
170
        $tWidth = $width ?: $this->width;
171
        $tHeight = $height ?: $this->height;
172
        $fileName = $this->getFileName();
173
        $path = $this->path . DIRECTORY_SEPARATOR . self::PREFIX_ORIGINAL . $fileName;
174
        $thumbPath = $this->path . DIRECTORY_SEPARATOR . self::PREFIX_THUMBNAIL . $fileName;
175
        return Image::resize($path, $tWidth, $tHeight)->save($thumbPath, ['quality' => $this->quality]);
176
    }
177
178
    /**
179
     * Crop
180
     * Обрежет по ширине на 150px, по высоте на 150px, начиная по оси X с отметки в 100px и по оси Y с отметки в 100px
181
     * @return ImageInterface
182
     */
183
    public function crop()
184
    {
185
        $fileName = $this->getFileName();
186
        $thumbPath = $this->path . DIRECTORY_SEPARATOR . self::PREFIX_THUMBNAIL . $fileName;
187
        $path = $this->path . DIRECTORY_SEPARATOR . $fileName;
188
        Image::crop($thumbPath, $this->cropWidth, $this->cropHeight, [$this->cropX, $this->cropY])
189
            ->save($path, ['quality' => $this->cropQuality]);
190
        return Image::thumbnail(
191
            $path,
192
            $this->avatarWidth,
193
            $this->avatarHeight
194
        )->save($path, ['quality' => $this->avatarQuality]);
195
    }
196
197
    /**
198
     * @param int|string $id
199
     * @return bool
200
     */
201
    public function isThumbFile($id)
202
    {
203
        $fileName = $this->getFileName();
204
        $path = $this->getPath($id) . DIRECTORY_SEPARATOR . self::PREFIX_THUMBNAIL . $fileName;
205
        if (file_exists($path)) {
206
            return true;
207
        }
208
        return false;
209
    }
210
211
    /**
212
     * Delete file
213
     * @param string|array $path
214
     * @return bool
215
     */
216
    public function delete($path)
217
    {
218
        if (is_array($path)) {
219
            foreach ($path as $item) {
220
                if (file_exists($item)) {
221
                    FileHelper::unlink($item);
222
                }
223
            }
224
        } elseif (file_exists($path)) {
225
            FileHelper::unlink($path);
226
        }
227
        return true;
228
    }
229
230
    /**
231
     * @param int|string $id
232
     * @return string
233
     */
234
    public function getPath($id)
235
    {
236
        $this->path = Yii::getAlias($this->directory . '/' . $id);
237
        return (string)$this->path;
238
    }
239
240
    /**
241
     * Create directory
242
     * @return bool
243
     * @throws Exception
244
     */
245
    public function createDirectory()
246
    {
247
        return FileHelper::createDirectory($this->path, 0777);
248
    }
249
}
250