e10587b1
Yarik
first commit
|
1
|
<?php
|
143c55a9
Yarik
Namespaces
|
2
3
4
5
6
7
8
9
10
11
12
13
|
namespace artweb\artbox\file\controllers;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\UploadedFile;
use artweb\artbox\file\models\ImageSizerForm;
use yii\web\Controller;
use Imagine\Gd\Imagine;
use Imagine\Image\Box;
use yii\imagine\Image;
class UploaderController extends Controller
|
e10587b1
Yarik
first commit
|
14
|
{
|
143c55a9
Yarik
Namespaces
|
15
16
17
18
19
20
21
22
23
24
25
|
public $enableCsrfValidation = false;
public function isBigger($width, $height, $w, $h)
{
if ($width > $w) {
return true;
} else if ($height > $h) {
return true;
}
return false;
|
e10587b1
Yarik
first commit
|
26
|
}
|
143c55a9
Yarik
Namespaces
|
27
28
29
30
31
32
33
34
|
private function getUserPath()
{
if (isset( Yii::$app->user->id )) {
return 'user_' . Yii::$app->user->id;
} else {
return 'guest';
}
|
e10587b1
Yarik
first commit
|
35
|
}
|
143c55a9
Yarik
Namespaces
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
private function resizeImg($w, $h, $imageAlias, $imageAliasSave)
{
$img = Image::getImagine()
->open(Yii::getAlias($imageAlias));
$size = $img->getSize();
$width = $size->getWidth();
$height = $size->getHeight();
$e_width = $w / $h;
$e_height = $h / $w;
$e1_width = $width / $height;
$e1_height = $height / $width;
if ($e_width < $e1_width) {
$new_width = $width * ( $e_width / $e1_width );
|
e10587b1
Yarik
first commit
|
57
|
$y = 0;
|
143c55a9
Yarik
Namespaces
|
58
|
$x = $width / 2 - ( $new_width / 2 );
|
e10587b1
Yarik
first commit
|
59
|
$width = $new_width;
|
143c55a9
Yarik
Namespaces
|
60
61
62
63
|
} else {
$new_height = $height * ( $e_height / $e1_height );
|
e10587b1
Yarik
first commit
|
64
|
$x = 0;
|
143c55a9
Yarik
Namespaces
|
65
|
$y = $height / 2 - ( $new_height / 2 );
|
e10587b1
Yarik
first commit
|
66
67
|
$height = $new_height;
}
|
143c55a9
Yarik
Namespaces
|
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
Image::crop(
$imageAlias,
$width,
$height,
[
$x,
$y,
]
)
->save(
Yii::getAlias($imageAliasSave),
[
'quality' => 100,
]
);
$imagine = new Imagine();
$imagine->open($imageAliasSave)
->resize(new Box($w, $h))
->save($imageAliasSave, [ 'flatten' => false ]);
|
e10587b1
Yarik
first commit
|
90
|
}
|
143c55a9
Yarik
Namespaces
|
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
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 ] . '/' . $rootDir[ 3 ] . '/';
$allFiles = scandir($row);
$allFiles = array_slice($allFiles, 2);
foreach ($allFiles as $oldFile) {
unlink($row . $oldFile);
}
|
e10587b1
Yarik
first commit
|
111
112
|
}
}
|
143c55a9
Yarik
Namespaces
|
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
public function actionDeleteImage()
{
$this->enableCsrfValidation = false;
$request = Yii::$app->request->post();
if ($request) {
if ($request[ 'old_img' ]) {
$this->deleteImages($request[ 'old_img' ]);
}
if (isset( $request[ 'action' ] ) && $request[ 'action' ] == 'save') {
$object = str_replace('-', '\\', $request[ 'model' ]);
$model = new $object;
$model = $model->findOne($request[ 'id' ]);
$model->$request[ 'field' ] = $request[ 'new_url' ];
$model->save();
}
|
e10587b1
Yarik
first commit
|
132
|
}
|
143c55a9
Yarik
Namespaces
|
133
134
135
136
137
138
139
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
|
}
public function actionDownloadPhoto()
{
$model = new ImageSizerForm();
$request = Yii::$app->request->post();
if ($request) {
$model->multi = isset( $request[ 'multi' ] ) ? 1 : 0;
$model->file = UploadedFile::getInstance($model, 'file');
$md5_file = md5_file($model->file->tempName) . rand(1, 1000);
$imgDir = Yii::getAlias('@storage/' . $this->getUserPath() . '/' . $md5_file . '/');
$imageOrigAlias = Yii::getAlias($imgDir . 'original' . '.' . $model->file->extension);
if (!is_dir($imgDir)) {
mkdir($imgDir, 0755, true);
}
$model->file->saveAs($imageOrigAlias);
if (isset( $request[ 'size' ] )) {
$request[ 'size' ] = ArrayHelper::toArray(json_decode($request[ 'size' ]));
foreach ($request[ 'size' ] as $size) {
if ($size[ 'width' ] && $size[ 'height' ]) {
$imageAlias = Yii::getAlias(
$imgDir . $size[ 'width' ] . 'x' . $size[ 'height' ] . '.' . $model->file->extension
);
$imageLink = '/storage/' . $this->getUserPath(
) . '/' . $md5_file . '/' . $size[ 'width' ] . 'x' . $size[ 'height' ] . '.' . $model->file->extension;
$this->resizeImg($size[ 'width' ], $size[ 'height' ], $imageOrigAlias, $imageAlias);
}
|
e10587b1
Yarik
first commit
|
178
|
}
|
143c55a9
Yarik
Namespaces
|
179
180
181
182
183
184
|
} else {
$imageLink = '/storage/' . $this->getUserPath(
) . '/' . $md5_file . '/' . 'original' . '.' . $model->file->extension;
|
e10587b1
Yarik
first commit
|
185
|
}
|
143c55a9
Yarik
Namespaces
|
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
if ($model->multi) {
$view = $this->renderPartial(
'/_gallery_item',
[
'item' => [ 'image' => $imageLink ],
'field' => $request[ 'field' ],
]
);
return json_encode(
[
'link' => $imageLink,
'view' => $view,
]
);
} else {
$view = $this->renderPartial(
'/_one_item',
[
'item' => [ 'image' => $imageLink ],
'field' => $request[ 'field' ],
]
);
return json_encode(
[
'link' => $imageLink,
'view' => $view,
]
);
}
|
e10587b1
Yarik
first commit
|
219
|
}
|
e10587b1
Yarik
first commit
|
220
|
}
|
143c55a9
Yarik
Namespaces
|
221
222
|
public function getex($filename)
|
e10587b1
Yarik
first commit
|
223
|
{
|
143c55a9
Yarik
Namespaces
|
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
$array = explode(".", $filename);
return array_pop($array);
}
public function actionImagesUpload()
{
if ($_FILES[ 'upload' ]) {
if (( $_FILES[ 'upload' ] == "none" ) OR ( empty( $_FILES[ 'upload' ][ 'name' ] ) )) {
$message = "Вы не выбрали файл";
} else if ($_FILES[ 'upload' ][ "size" ] == 0 OR $_FILES[ 'upload' ][ "size" ] > 2050000) {
$message = "Размер файла не соответствует нормам";
} else if (( $_FILES[ 'upload' ][ "type" ] != "image/jpeg" ) AND ( $_FILES[ 'upload' ][ "type" ] != "image/jpeg" ) AND ( $_FILES[ 'upload' ][ "type" ] != "image/png" ) AND ( $_FILES[ 'upload' ][ 'type' ] != 'image/gif' )) {
$message = "Допускается загрузка только картинок JPG и PNG.";
} else if (!is_uploaded_file($_FILES[ 'upload' ][ "tmp_name" ])) {
$message = "Что-то пошло не так. Попытайтесь загрузить файл ещё раз.";
} else {
$filename = $_FILES[ 'upload' ][ 'name' ];
$name = $_FILES[ 'upload' ][ 'name' ] . '.' . $this->getex($filename);
$path = "../../storage/" . $this->getUserPath() . "/images/";
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
move_uploaded_file($_FILES[ 'upload' ][ 'tmp_name' ], $path . $name);
$full_path = '/storage/' . $this->getUserPath() . '/images/' . $name;
$message = "Файл " . $_FILES[ 'upload' ][ 'name' ] . " загружен";
|
e10587b1
Yarik
first commit
|
255
|
}
|
143c55a9
Yarik
Namespaces
|
256
257
|
$callback = $_REQUEST[ 'CKEditorFuncNum' ];
echo '<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction("' . $callback . '", "' . $full_path . '", "' . $message . '" );</script>';
|
e10587b1
Yarik
first commit
|
258
|
}
|
e10587b1
Yarik
first commit
|
259
|
}
|
143c55a9
Yarik
Namespaces
|
260
261
|
}
|