Blame view

backend/components/base/BaseController.php 2.69 KB
54ada04a   Mihail   add base classes ...
1
2
3
4
5
6
7
8
9
  <?php
  /**
   * Created by PhpStorm.
   * User: Cibermag
   * Date: 31.08.2015
   * Time: 9:58
   */
  namespace backend\components\base;
  
4828b892   Mihail   after merge with ...
10
11
12
  use Yii;
  use yii\web\UploadedFile;
  use backend\models\ImageSizerForm;
54ada04a   Mihail   add base classes ...
13
  use yii\web\Controller;
4828b892   Mihail   after merge with ...
14
15
16
  use Imagine\Gd\Imagine;
  use Imagine\Image\Box;
  use yii\imagine\Image;
54ada04a   Mihail   add base classes ...
17
18
19
  
  class BaseController extends Controller {
  
4828b892   Mihail   after merge with ...
20
21
22
23
24
25
26
27
28
      public function beforeAction($action) {
          $this->enableCsrfValidation = false;
          return parent::beforeAction($action);
      }
  
      private function resizeImg($w, $h, $imageAlias,$imageAliasSave){
          $img = Image::getImagine()->open(Yii::getAlias($imageAlias));
  
          $size = $img->getSize();
4828b892   Mihail   after merge with ...
29
  
992d7eaa   Administrator   Importers CRUD
30
31
32
33
34
35
36
37
38
39
40
41
42
          $width = $size->getWidth();
          $height = $size->getHeight();
  
          if($width > $height) {
              $y = 0;
              $x = ($width - $height) / 2;
              $smallestSide = $height;
          } else {
              $x = 0;
              $y = ($height - $width) / 2;
              $smallestSide = $width;
          }
  
4c977682   Administrator   Importers CRUD
43
44
45
          Image::crop($imageAlias, $smallestSide, $smallestSide,[$x,$y])
              ->save(Yii::getAlias($imageAliasSave), ['quality' =>
                  100]);
992d7eaa   Administrator   Importers CRUD
46
47
  
  
4c977682   Administrator   Importers CRUD
48
49
50
51
          $imagine = new Imagine();
          $imagine->open($imageAliasSave)
              ->resize(new Box($w, $h))
              ->save($imageAliasSave, array('flatten' => false));
4828b892   Mihail   after merge with ...
52
  
4828b892   Mihail   after merge with ...
53
54
  
  
4828b892   Mihail   after merge with ...
55
56
57
  
  
      }
992d7eaa   Administrator   Importers CRUD
58
  
4828b892   Mihail   after merge with ...
59
60
61
62
63
  
      public function actionDownloadPhoto()
      {
  
          $model = new ImageSizerForm();
ea4ecf3d   Mihail   console csv parsing
64
  
4828b892   Mihail   after merge with ...
65
66
67
68
69
          if ($model->load(Yii::$app->request->post())) {
  
              $model->file = UploadedFile::getInstance($model, 'file');
  
              $md5_file = md5_file($model->file->tempName);
4c977682   Administrator   Importers CRUD
70
  
4828b892   Mihail   after merge with ...
71
              $imgDir = Yii::getAlias('@storage/'.$md5_file.'/');
4c977682   Administrator   Importers CRUD
72
73
  
              $imageOrigAlias = Yii::getAlias($imgDir.'original'.'.'.$model->file->extension);
ea4ecf3d   Mihail   console csv parsing
74
  
4828b892   Mihail   after merge with ...
75
76
77
              if(!is_dir($imgDir)) {
                  mkdir($imgDir, 0755, true);
              }
4c977682   Administrator   Importers CRUD
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
  
              $model->file->saveAs($imageOrigAlias);
  
              if($model->width && $model->height){
  
                  $imageAlias = Yii::getAlias($imgDir.$model->width.'x'.$model->height.'.'.$model->file->extension);
  
                  $imageLink = '/storage/'.$md5_file.'/'.$model->width.'x'.$model->height.'.'.$model->file->extension;
  
                  $this->resizeImg($model->width, $model->height, $imageOrigAlias,$imageAlias);
  
              } else {
  
                  $imageLink = $imageOrigAlias;
  
              }
  
              if($model->multi){
  
                  return json_encode(['link'=>$imageLink]);
  
              } else {
                  $view = $this->renderPartial('@app/components/views/_gallery_item', [
                      'item' => ['image'=>$imageLink],
                  ]);
  
                  return json_encode(['link'=>$imageLink, 'view' =>$view]);
              }
  
4828b892   Mihail   after merge with ...
107
108
109
  
          }
      }
54ada04a   Mihail   add base classes ...
110
111
  
  }