Blame view

controllers/UploaderController.php 9.91 KB
e10587b1   Yarik   first commit
1
  <?php

143c55a9   Yarik   Namespaces
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
      /**

       * Created by PhpStorm.

       * User: Cibermag

       * Date: 31.08.2015

       * Time: 9:58

       */

      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
20
      {

143c55a9   Yarik   Namespaces
21
22
23
24
25
26
27
28
29
30
31
          

          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
32
          }

143c55a9   Yarik   Namespaces
33
34
35
36
37
38
39
40
          

          private function getUserPath()

          {

              if (isset( Yii::$app->user->id )) {

                  return 'user_' . Yii::$app->user->id;

              } else {

                  return 'guest';

              }

e10587b1   Yarik   first commit
41
          }

143c55a9   Yarik   Namespaces
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
          

          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
63
                  $y = 0;

143c55a9   Yarik   Namespaces
64
                  $x = $width / 2 - ( $new_width / 2 );

e10587b1   Yarik   first commit
65
                  $width = $new_width;

143c55a9   Yarik   Namespaces
66
67
68
69
                  

              } else {

                  

                  $new_height = $height * ( $e_height / $e1_height );

e10587b1   Yarik   first commit
70
                  $x = 0;

143c55a9   Yarik   Namespaces
71
                  $y = $height / 2 - ( $new_height / 2 );

e10587b1   Yarik   first commit
72
73
                  $height = $new_height;

              }

143c55a9   Yarik   Namespaces
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
              

              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
96
          }

143c55a9   Yarik   Namespaces
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
          

          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
117
118
              }

          }

143c55a9   Yarik   Namespaces
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
          

          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
138
              }

143c55a9   Yarik   Namespaces
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
178
179
180
181
182
183
              

          }

          

          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
184
                      }

143c55a9   Yarik   Namespaces
185
186
187
188
189
190
                      

                  } else {

                      

                      $imageLink = '/storage/' . $this->getUserPath(

                          ) . '/' . $md5_file . '/' . 'original' . '.' . $model->file->extension;

                      

e10587b1   Yarik   first commit
191
                  }

143c55a9   Yarik   Namespaces
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
219
220
221
222
223
224
                  

                  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
225
              }

e10587b1   Yarik   first commit
226
          }

143c55a9   Yarik   Namespaces
227
228
          

          public function getex($filename)

e10587b1   Yarik   first commit
229
          {

143c55a9   Yarik   Namespaces
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
255
256
257
258
259
260
              $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
261
                  }

143c55a9   Yarik   Namespaces
262
263
                  $callback = $_REQUEST[ 'CKEditorFuncNum' ];

                  echo '<script type="text/javascript">window.parent.CKEDITOR.tools.callFunction("' . $callback . '", "' . $full_path . '", "' . $message . '" );</script>';

e10587b1   Yarik   first commit
264
              }

e10587b1   Yarik   first commit
265
          }

143c55a9   Yarik   Namespaces
266
267
          

      }