-
Notifications
You must be signed in to change notification settings - Fork 34
/
UploadImage.php
108 lines (101 loc) · 3.38 KB
/
UploadImage.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
<?php
namespace mdm\upload;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\UploadedFile;
use yii\validators\ImageValidator;
use yii\imagine\Image;
/**
* Description of UploadImage
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
class UploadImage
{
/**
* @param string $name
* @param array $options
* @return boolean
*/
public static function store($name, $options = [])
{
$multiple = ArrayHelper::remove($options, 'multiple', false);
$crop = ArrayHelper::remove($options, 'crop');
$resize = ArrayHelper::remove($options, 'resize');
$rules = ArrayHelper::remove($options, 'rules', []);
$validator = new ImageValidator();
if ($crop) {
$options['saveCallback'] = function ($model) use($crop, $rules) {
return static::cropImage($model, $crop, $rules);
};
} elseif ($resize) {
$options['saveCallback'] = function ($model) use($resize) {
return static::resizeImage($model, $resize);
};
} elseif (!empty($rules)) {
Yii::configure($validator, $rules);
}
if ($multiple) {
$files = UploadedFile::getInstancesByName($name);
foreach ($files as $file) {
if (!$validator->validate($file)) {
return false;
}
}
$result = [];
foreach ($files as $file) {
if (false !== ($model = FileModel::saveAs($file, $options))) {
$result[] = $model->id;
} else {
return false;
}
}
return $result;
} else {
$file = UploadedFile::getInstanceByName($name);
if ($validator->validate($file) && ($model = FileModel::saveAs($file, $options)) !== false) {
return $model->id;
}
}
return false;
}
/**
*
* @param FileModel $model
* @param integer $width
* @param integer $height
*/
public static function resizeImage($model, $resize)
{
$width = ArrayHelper::getValue($resize, 'width');
$height = ArrayHelper::getValue($resize, 'height');
$image = Image::thumbnail($model->file->tempName, $width, $height);
$image->save($model->filename);
$model->size = filesize($model->filename);
return true;
}
/**
*
* @param FileModel $model
* @param array $crop
*/
public static function cropImage($model, $crop, $rules = [])
{
$rasio = ArrayHelper::getValue($crop, 'er', 1);
foreach (['w', 'h', 'x', 'y'] as $prop) {
$crop[$prop] = $rasio * $crop[$prop];
}
$inValid = isset($rules['minWidth']) && $crop['w'] < $rules['minWidth'] || isset($rules['minHeight']) && $crop['h']
< $rules['minHeight'] || isset($rules['maxWidth']) && $crop['w'] > $rules['maxWidth'] || isset($rules['maxHeight'])
&& $crop['h'] > $rules['maxHeight'];
if ($inValid) {
Yii::$app->session->setFlash('error', 'Invalid image size');
return false;
}
$image = Image::crop($model->file->tempName, $crop['w'], $crop['h'], [$crop['x'], $crop['y']]);
$image->save($model->filename);
$model->size = filesize($model->filename);
return true;
}
}