Blame view

common/modules/file/components/UploaderComponent.php 1.45 KB
6676fbc7   Administrator   28.04.16 перенес ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  <?php
  
  namespace common\modules\file\components;
  use Yii;
  use yii\base\Component;
  use Imagine\Gd\Imagine;
  use Imagine\Image\Box;
  use yii\imagine\Image;
  
  class UploaderComponent extends Component {
  
      public function isBigger($width,$height,$w,$h)
      {
          if($width>$w){
              return true;
          }else if($height >$h) {
              return true;
          }
          return false;
      }
  
      public 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);
  
              $y = 0;
              $x = $width/ 2-($new_width/2);
              $width = $new_width;
  
          }else {
  
              $new_height = $height*($e_height/$e1_height);
              $x = 0;
              $y = $height/2-($new_height/2);
              $height = $new_height;
          }
  
  
  
  
          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, array('flatten' => false));
  
  
      }
  
  
  }