Blame view

backend/components/base/BaseController.php 2.71 KB
5fb8f6f6   Administrator   update rep
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;
  
5051b0e8   Administrator   Importers CRUD
10
11
12
  use Yii;
  use yii\web\UploadedFile;
  use backend\models\ImageSizerForm;
5fb8f6f6   Administrator   update rep
13
  use yii\web\Controller;
5051b0e8   Administrator   Importers CRUD
14
15
16
  use Imagine\Gd\Imagine;
  use Imagine\Image\Box;
  use yii\imagine\Image;
9c00bbba   Administrator   Importers CRUD
17
  
5fb8f6f6   Administrator   update rep
18
19
  class BaseController extends Controller {
  
16ab6b2a   Administrator   Importers CRUD
20
21
22
23
24
      public function beforeAction($action) {
          $this->enableCsrfValidation = false;
          return parent::beforeAction($action);
      }
  
9c00bbba   Administrator   Importers CRUD
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
      private function resizeImg($w, $h, $imageAlias,$imageAliasSave){
          $img = Image::getImagine()->open(Yii::getAlias($imageAlias));
  
          $size = $img->getSize();
          $ratio = $size->getWidth()/$size->getHeight();
  
          $height = $h;
          $width = round($height * $ratio);
  
  
          $imagine = new Imagine();
  
          $imagine->open($imageAlias)
              ->resize(new Box($width, $h))
              ->save($imageAlias, array('flatten' => false));
  
  
          Image::crop($imageAlias, $w, $h,[($width/2)-($w/2),0])
              ->save(Yii::getAlias($imageAliasSave), ['quality' =>
                  100]);
  
  
      }
5fb8f6f6   Administrator   update rep
48
  
5051b0e8   Administrator   Importers CRUD
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
  //    private function resizeImg($w, $h, $filepath,$newfilepath){
  //        list($orig_width, $orig_height) = getimagesize($filepath);
  //        $width = $orig_width;
  //        $height = $orig_height;
  //        if($width > $height) {
  //            $y = 0;
  //            $x = ($width - $height) / 2;
  //            $smallestSide = $height;
  //        } else {
  //            $x = 0;
  //            $y = ($height - $width) / 2;
  //            $smallestSide = $width;
  //        }
  //
  //        $image_p = imagecreatetruecolor($w, $h);
  //
  //        $image = imagecreatefromjpeg($filepath);
  //
  //        imagecopyresampled($image_p, $image, 0, 0, $x, $y,
  //            $w, $h, $smallestSide, $smallestSide);
  //
  //        imagejpeg($image_p, $newfilepath);
  //
  //
  //    }
  
16ab6b2a   Administrator   Importers CRUD
75
76
      public function actionDownloadPhoto()
      {
16ab6b2a   Administrator   Importers CRUD
77
  
5051b0e8   Administrator   Importers CRUD
78
79
          $model = new ImageSizerForm();
      //die(print_r(Yii::$app->request->post()));
16ab6b2a   Administrator   Importers CRUD
80
81
82
83
84
85
          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.'/');
5051b0e8   Administrator   Importers CRUD
86
87
              $imageAlias = Yii::getAlias($imgDir.$model->width.'x'.$model->height.'.'.$model->file->extension);
              $imageLink = '/storage/'.$md5_file.$model->width.'x'.$model->height.'.'.$model->file->extension;
16ab6b2a   Administrator   Importers CRUD
88
              if(!is_dir($imgDir)) {
5051b0e8   Administrator   Importers CRUD
89
                  mkdir($imgDir, 0755, true);
16ab6b2a   Administrator   Importers CRUD
90
91
              }
              $model->file->saveAs($imageAlias);
5051b0e8   Administrator   Importers CRUD
92
              $this->resizeImg($model->width, $model->height, $imageAlias,$imageAlias);
16ab6b2a   Administrator   Importers CRUD
93
94
95
96
97
              return json_encode(['link'=>$imageLink]);
  
          }
      }
  
5fb8f6f6   Administrator   update rep
98
  }