e07bc75d
Administrator
first commit from...
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php
namespace backend\components;
use yii\web\UploadedFile;
use backend\models\ImageSizerForm;
use yii\base\Widget;
use Imagine\Image\Box;
use yii\imagine\Image;
use Yii;
use Imagine\Gd\Imagine;
|
c6152cd6
Administrator
VItaliy 25.11.2015
|
12
|
class ImageResizer extends Widget
|
e07bc75d
Administrator
first commit from...
|
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
|
{
public $height;
public $width;
public function init(){
parent::init();
}
public function run(){
$model = new ImageSizerForm();
$request = Yii::$app->request->post();
if ($request) {
if(isset($request['old_img'])){
$this->deleteImages($request['old_img']);
}
$model->file = UploadedFile::getInstance($model, 'file');
|
c6152cd6
Administrator
VItaliy 25.11.2015
|
39
40
41
42
|
if(!$model->file){
return json_encode(["error"=>"Не указан файл"]);
}
|
e07bc75d
Administrator
first commit from...
|
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
|
$md5_file = md5_file($model->file->tempName);
$imgDir = Yii::getAlias('@storage/'.$md5_file.'/');
$imageOrigAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file->extension);
if(!is_dir($imgDir)) {
mkdir($imgDir, 0755, true);
}
$model->file->saveAs($imageOrigAlias);
if($request['width'] && $request['height']){
$imageAlias = Yii::getAlias($imgDir.$request['width'].'x'.$request['height'].'.'.$model->file->extension);
$imageLink = '/storage/'.$md5_file.'/'.$request['width'].'x'.$request['height'].'.'.$model->file->extension;
$this->resizeImg($request['width'],$request['height'], $imageOrigAlias,$imageAlias);
} else {
$imageLink = '/storage/'.$md5_file.'/'.'original'.'.'.$model->file->extension;
}
if($model->multi){
// $view = $this->renderPartial('@app/components/views/_gallery_item', [
// 'item' => ['image'=>$imageLink],
// ]);
//
// return json_encode(['link'=>$imageLink, 'view' =>$view]);
} else {
$p1[0] = "<img style='height:160px' src='$imageLink' class='file-preview-image'>";
return json_encode(['success','initialPreview' => $p1, 'append' => false, 'name' =>$imageLink,]);
}
} else {
return json_encode(['error']);
}
}
public function isBigger($width,$height,$w,$h)
{
if($width>$w){
return true;
}else if($height >$h) {
return true;
}
return false;
}
|
e07bc75d
Administrator
first commit from...
|
105
106
107
108
109
110
111
112
113
114
115
116
|
public function resizeImg($w, $h, $imageAlias,$imageAliasSave)
{
$img = Image::getImagine()->open(Yii::getAlias($imageAlias));
$size = $img->getSize();
$width = $size->getWidth();
$height = $size->getHeight();
|
66dd9e9a
Administrator
first commit from...
|
117
118
|
$e_width = $w/$h;
$e_height = $h/$w;
|
e07bc75d
Administrator
first commit from...
|
119
|
|
66dd9e9a
Administrator
first commit from...
|
120
121
|
$e1_width = $width/$height;
$e1_height = $height/$width;
|
e07bc75d
Administrator
first commit from...
|
122
|
|
66dd9e9a
Administrator
first commit from...
|
123
124
125
126
|
if($this->isBigger($width,$height,$w,$h)){
if($e_width<$e1_width){
$new_width = $width*($e_width/$e1_width);
$width = $new_width;
|
e07bc75d
Administrator
first commit from...
|
127
|
}else {
|
66dd9e9a
Administrator
first commit from...
|
128
129
|
$new_height = $height*($e_height/$e1_height);
$height = $new_height;
|
e07bc75d
Administrator
first commit from...
|
130
|
}
|
66dd9e9a
Administrator
first commit from...
|
131
132
|
|
e07bc75d
Administrator
first commit from...
|
133
134
135
136
137
138
|
} else {
$img->save($imageAliasSave, array('flatten' => false));
return true;
}
|
66dd9e9a
Administrator
first commit from...
|
139
|
Image::crop($imageAlias, $width, $height,[0,0])
|
e07bc75d
Administrator
first commit from...
|
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
179
180
181
182
183
184
185
|
->save(Yii::getAlias($imageAliasSave), ['quality' =>
100]);
$imagine = new Imagine();
$imagine->open($imageAliasSave)
->resize(new Box($w, $h))
->save($imageAliasSave, array('flatten' => false));
}
private function deleteImages($old_img){
if(!empty($old_img) && file_exists($_SERVER['DOCUMENT_ROOT'].$old_img)){
$rootDir = explode("/", $old_img);
$row = $_SERVER['DOCUMENT_ROOT'].'/'.$rootDir[1].'/'.$rootDir[2].'/';
$allFiles = scandir($row);
$allFiles = array_slice($allFiles, 2);
foreach($allFiles as $oldFile){
unlink($row.$oldFile);
}
}
}
public function actionDeleteImage(){
$old_img = Yii::$app->request->post('old_img');
if ($old_img) {
$this->deleteImages($old_img);
}
}
}
|