Blame view

common/behaviors/ShowImage.php 5.3 KB
9217ef8e   Administrator   09.02.16
1
  <?php
55bd0bb9   Yarik   Commit
2
      
f0a961be   Yarik   test
3
      namespace common\behaviors;
55bd0bb9   Yarik   Commit
4
      
f0a961be   Yarik   test
5
6
7
      use common\modules\file\components\UploaderComponent;
      use yii;
      use yii\base\Behavior;
55bd0bb9   Yarik   Commit
8
9
      use yii\imagine\Image;
      
f0a961be   Yarik   test
10
11
      class ShowImage extends Behavior
      {
55bd0bb9   Yarik   Commit
12
          
f0a961be   Yarik   test
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
          /**
           * 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';
              }
55bd0bb9   Yarik   Commit
28
              
f0a961be   Yarik   test
29
30
31
32
33
34
              if($width == 'original') {
                  $preg = '/\/(.[^\/]*)$/';
                  preg_match('/\.(.[^.]*)$/', $dir, $type);
                  if(isset( $type[ 1 ] )) {
                      $dir = preg_replace($preg, '/original.' . $type[ 1 ], $dir);
                  }
55bd0bb9   Yarik   Commit
35
                  
f0a961be   Yarik   test
36
37
38
39
40
41
42
43
              } 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)) {
55bd0bb9   Yarik   Commit
44
                          
f0a961be   Yarik   test
45
                          $original = $storage . dirname($dir) . '/original.' . $type[ 1 ];
38ffb9db   Yarik   test
46
                          $resizer = new UploaderComponent();
f0a961be   Yarik   test
47
                          if(file_exists($original)) {
f0a961be   Yarik   test
48
                              $resizer->resizeImg($width, $height, $original, $filename);
f0a961be   Yarik   test
49
                          } else {
55bd0bb9   Yarik   Commit
50
51
52
                              $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);
38ffb9db   Yarik   test
53
                              }
55bd0bb9   Yarik   Commit
54
                              return "/storage/imageNotFound" . $width . "x" . $height . ".jpg";
f0a961be   Yarik   test
55
                          }
55bd0bb9   Yarik   Commit
56
                          
6676fbc7   Administrator   28.04.16 перенес ...
57
                      }
55bd0bb9   Yarik   Commit
58
                      
6676fbc7   Administrator   28.04.16 перенес ...
59
                  }
55bd0bb9   Yarik   Commit
60
                  
df799956   Administrator   16.03.16
61
              }
f0a961be   Yarik   test
62
              return $dir;
34e7b1a4   Administrator   15.02.16
63
          }
55bd0bb9   Yarik   Commit
64
          
f0a961be   Yarik   test
65
66
67
68
69
70
71
          /**
           * @param string $array String to split
           *
           * @return array Array of image paths
           */
          function ShowGallery($array)
          {
55bd0bb9   Yarik   Commit
72
              
f0a961be   Yarik   test
73
74
75
76
77
              $gallery = explode(',', $array);
              if(is_array($gallery)) {
                  array_splice($gallery, -1);
                  return $gallery;
              } else {
55bd0bb9   Yarik   Commit
78
                  return [];
f0a961be   Yarik   test
79
              }
55bd0bb9   Yarik   Commit
80
              
f0a961be   Yarik   test
81
          }
55bd0bb9   Yarik   Commit
82
83
84
85
          
          public function ShowAvatar($dir, $width, $height = NULL)
          {
              if(empty( $dir )) {
cefc2aa6   Yarik   test
86
87
88
89
90
                  return '/images/avatar-bg.png';
              } else {
                  return $this->minImg($dir, $width, $height);
              }
          }
55bd0bb9   Yarik   Commit
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
      
          /**
           * Add watermark to image
           *
           * @param string $path image path
           *
           * @return yii\base\Component
           */
          public function watermark($path)
          {
              $watermark_path = Yii::getAlias('@webroot/images/watermark.png');
              $image = Image::getImagine()
                            ->open($path);
              $watermark = Image::getImagine()
                                ->open($watermark_path);
              $image_size = $image->getSize();
              $watermark_size = $watermark->getSize();
              if($image_size->getWidth() < ($watermark_size->getWidth() + 20) || ($image_size->getHeight() + 20) < $watermark_size->getHeight()) {
                  return $this->owner;
              }
              $position_left = $image_size->getWidth() - ($watermark_size->getWidth() + 10);
              Image::watermark($path, $watermark_path, [$position_left, 10])->save(preg_replace('/^(.*)(\.\w+)$/', '${1}_watermark${2}', $path));
              return $this->owner;
          }
      
          function getWatermark($path, $width, $height = NULL)
          {
              if($width == 'original') {
                  $path_original = $this->minImg($path, 'original');
                  $path_watermark = str_replace('original', 'original_watermark', $path_original);
                  if(!file_exists(Yii::getAlias('@documentRoot').$path_watermark)) {
                      $this->watermark(Yii::getAlias('@documentRoot').$path_original);
                  }
                  return $path_watermark;
              } else {
                  $path_original = $this->minImg($path, $width, $height);
                  preg_match('/^.*(\.\w+)$/', $path_original, $extension);
                  $path_watermark = $path_original;
                  if(isset($extension[1])) {
                      $path_watermark = str_replace($extension[1], '_watermark'.$extension[1], $path_original);
                  }
                  if(!file_exists(Yii::getAlias('@documentRoot').$path_watermark)) {
                      $this->watermark(Yii::getAlias('@documentRoot').$path_original);
                  }
                  return $path_watermark;
              }
          }
          
f0a961be   Yarik   test
139
      }