Blame view

backend/components/base/BaseController.php 2.33 KB
693c46cb   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;
  
f7ab7644   Mihail   after merge with ...
10
11
12
  use Yii;
  use yii\web\UploadedFile;
  use backend\models\ImageSizerForm;
693c46cb   Mihail   add base classes ...
13
  use yii\web\Controller;
f7ab7644   Mihail   after merge with ...
14
15
16
  use Imagine\Gd\Imagine;
  use Imagine\Image\Box;
  use yii\imagine\Image;
693c46cb   Mihail   add base classes ...
17
18
19
  
  class BaseController extends Controller {
  
f7ab7644   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();
f7ab7644   Mihail   after merge with ...
29
  
3a9830b4   Administrator   Importers CRUD
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
          $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;
          }
  
  
  
  
  //        die('$width = '.$width.','.'$height = '.$height.','.'$y = '.$y.','.'$x = '.$x.','.'$smallestSide = '.$smallestSide.',' );
  //
  //        $ratio = $size->getWidth()/$size->getHeight();
  //
  //        $height = $h;
  //        $width = round($height * $ratio);
  
          $image_p = imagecreatetruecolor($w, $h);
  
          $image = imagecreatefromjpeg($imageAlias);
f7ab7644   Mihail   after merge with ...
56
  
3a9830b4   Administrator   Importers CRUD
57
58
59
60
          imagecopyresampled($image_p, $image, 0, 0, $x, $y,
              $w, $h, $smallestSide, $smallestSide);
  
          imagejpeg($image_p, $imageAliasSave);
f7ab7644   Mihail   after merge with ...
61
  
f7ab7644   Mihail   after merge with ...
62
  
f7ab7644   Mihail   after merge with ...
63
64
  
  
f7ab7644   Mihail   after merge with ...
65
66
67
  
  
      }
3a9830b4   Administrator   Importers CRUD
68
  
f7ab7644   Mihail   after merge with ...
69
70
71
72
73
  
      public function actionDownloadPhoto()
      {
  
          $model = new ImageSizerForm();
df629228   Mihail   console csv parsing
74
  
f7ab7644   Mihail   after merge with ...
75
76
77
78
79
80
81
          if ($model->load(Yii::$app->request->post())) {
  
              $model->file = UploadedFile::getInstance($model, 'file');
  
              $md5_file = md5_file($model->file->tempName);
              $imgDir = Yii::getAlias('@storage/'.$md5_file.'/');
              $imageAlias = Yii::getAlias($imgDir.$model->width.'x'.$model->height.'.'.$model->file->extension);
3a9830b4   Administrator   Importers CRUD
82
              $imageLink = '/storage/'.$md5_file.'/'.$model->width.'x'.$model->height.'.'.$model->file->extension;
df629228   Mihail   console csv parsing
83
  
f7ab7644   Mihail   after merge with ...
84
85
86
87
88
89
90
91
92
              if(!is_dir($imgDir)) {
                  mkdir($imgDir, 0755, true);
              }
              $model->file->saveAs($imageAlias);
              $this->resizeImg($model->width, $model->height, $imageAlias,$imageAlias);
              return json_encode(['link'=>$imageLink]);
  
          }
      }
693c46cb   Mihail   add base classes ...
93
94
  
  }