Blame view

common/behaviors/ShowImage.php 2.93 KB
9217ef8e   Administrator   09.02.16
1
2
  <?php
  
f0a961be   Yarik   test
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
      namespace common\behaviors;
  
      use common\modules\file\components\UploaderComponent;
      use yii;
      use yii\base\Behavior;
  
      class ShowImage extends Behavior
      {
  
          /**
           * Resize image and return its path.
           *
           * @param string              $dir    Original image path
           * @param integer|string      $width  New image width
           * @param null|integer|string $height New image height, set NULL to save ratio
           *
           * @return string Resized image path
           * @throws \Exception
           */
          function minImg($dir, $width, $height = NULL)
          {
              if(empty( $dir )) {
                  return '/images/imageNotFound.jpg';
              }
9217ef8e   Administrator   09.02.16
27
  
f0a961be   Yarik   test
28
29
30
31
32
33
              if($width == 'original') {
                  $preg = '/\/(.[^\/]*)$/';
                  preg_match('/\.(.[^.]*)$/', $dir, $type);
                  if(isset( $type[ 1 ] )) {
                      $dir = preg_replace($preg, '/original.' . $type[ 1 ], $dir);
                  }
9217ef8e   Administrator   09.02.16
34
  
f0a961be   Yarik   test
35
36
37
38
39
40
41
42
              } else {
                  $preg = '/\/(.[^\/]*)$/';
                  preg_match('/\.(.[^.]*)$/', $dir, $type);
                  if(isset( $type[ 1 ] )) {
                      $dir = preg_replace($preg, '/' . $width . 'x' . $height . '.' . $type[ 1 ], $dir);
                      $storage = dirname(yii\helpers\Url::to('@storage'));
                      $filename = $storage . $dir;
                      if(!file_exists($filename)) {
1aca8918   Administrator   09.02.16
43
  
f0a961be   Yarik   test
44
                          $original = $storage . dirname($dir) . '/original.' . $type[ 1 ];
38ffb9db   Yarik   test
45
                          $resizer = new UploaderComponent();
f0a961be   Yarik   test
46
                          if(file_exists($original)) {
f0a961be   Yarik   test
47
                              $resizer->resizeImg($width, $height, $original, $filename);
f0a961be   Yarik   test
48
                          } else {
38ffb9db   Yarik   test
49
50
51
52
53
54
55
56
  
                              $imageNotFound = yii\helpers\Url::to('@storage')."/imageNotFound".$width."x".$height.".jpg";
                              if(!file_exists($imageNotFound)){
                                   $resizer->resizeImg($width, $height, yii\helpers\Url::to('@storage')."/imageNotFound.jpg", $imageNotFound );
                              }
                              return "/storage/imageNotFound".$width."x".$height.".jpg";
  
  
f0a961be   Yarik   test
57
                          }
df799956   Administrator   16.03.16
58
  
6676fbc7   Administrator   28.04.16 перенес ...
59
60
61
62
                      }
  
                  }
  
df799956   Administrator   16.03.16
63
64
              }
  
f0a961be   Yarik   test
65
              return $dir;
9217ef8e   Administrator   09.02.16
66
  
34e7b1a4   Administrator   15.02.16
67
68
          }
  
f0a961be   Yarik   test
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
          /**
           * @param string $array String to split
           *
           * @return array Array of image paths
           */
          function ShowGallery($array)
          {
  
              $gallery = explode(',', $array);
              if(is_array($gallery)) {
                  array_splice($gallery, -1);
                  return $gallery;
              } else {
                  return [ ];
              }
34e7b1a4   Administrator   15.02.16
84
  
f0a961be   Yarik   test
85
          }
1a7bb1e7   Administrator   09.02.16
86
  
cefc2aa6   Yarik   test
87
88
89
90
91
92
93
94
          public function ShowAvatar($dir, $width, $height = NULL) {
              if(empty($dir)) {
                  return '/images/avatar-bg.png';
              } else {
                  return $this->minImg($dir, $width, $height);
              }
          }
  
f0a961be   Yarik   test
95
      }