Blame view

common/components/artboximage/ArtboxImageHelper.php 6.1 KB
4ca21c3e   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  <?php
  
  namespace common\components\artboximage;
  
  use Yii;
  use yii\base\Object;
  use yii\helpers\ArrayHelper;
  use yii\helpers\Html;
  
  class ArtboxImageHelper extends Object {
      /** @var  ArtboxImage $imageDriver */
      private static $imageDriver;
      private static $presets;
  
      public function getDriver() {
          if (empty(self::$imageDriver)) {
              self::$imageDriver = Yii::$app->artboximage;
          }
          return self::$imageDriver;
      }
9ceae125   Alexey Boroda   -Lazy load (now n...
21
22
23
24
25
26
27
      
      /**
       * Returns the preset from config array
       * @param $preset
       *
       * @return null
       */
4ca21c3e   Alexey Boroda   first commit
28
29
30
31
32
33
34
      public function getPreset($preset) {
  
          if (empty(self::$presets)) {
              self::$presets = self::getDriver()->presets;
          }
          return empty(self::$presets[$preset]) ? null : self::$presets[$preset];
      }
9ceae125   Alexey Boroda   -Lazy load (now n...
35
36
37
38
39
40
41
42
43
      
      /**
       * Gets html image tag with needed image
       * @param       $file
       * @param       $preset
       * @param array $imgOptions
       *
       * @return string
       */
4ca21c3e   Alexey Boroda   first commit
44
45
46
47
      public static function getImage($file, $preset, $imgOptions = []) {
          $preset_alias = is_array($preset) ? array_keys($preset)[0] : null;
          return Html::img(self::getImageSrc($file, $preset, $preset_alias), $imgOptions);
      }
9ceae125   Alexey Boroda   -Lazy load (now n...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
      
      public static function getLazyDiv($file, $preset, $options = [], $content = '') {
          $preset_alias = is_array($preset) ? array_keys($preset)[0] : null;
          if(!empty($options['class'])) {
              $options['class'] = $options['class'] . ' lazy';
          } else {
              $options['class'] = 'lazy';
          }
          $options['data-src'] = self::getImageSrc($file, $preset, $preset_alias);
          return Html::tag('div', $content, $options);
      }
      
      /**
       * Gets path to image with selected preset
       * @param      $file
       * @param      $preset
       * @param null $preset_alias
       *
       * @return bool|mixed
       */
4ca21c3e   Alexey Boroda   first commit
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
      public static function getImageSrc($file, $preset, $preset_alias = null) {
          if (is_string($preset)) {
              $preset_alias = $preset;
              $preset = self::getPreset($preset);
          }
          if (empty($preset) || empty($preset_alias)) {
              return $file;
          }
  
          $filePath = self::getPathFromUrl($file);
          if (!file_exists($filePath) || !preg_match('#^(.*)\.(' . self::getExtensionsRegexp() . ')$#', $file, $matches)) {
              return $file;
          }
          return self::getPresetUrl($filePath, $preset, $preset_alias);
      }
9ceae125   Alexey Boroda   -Lazy load (now n...
83
84
85
86
87
88
89
90
91
      
      /**
       * @param        $model
       * @param        $modelField
       * @param string $formField
       * @param bool   $multiple
       *
       * @return mixed
       */
4ca21c3e   Alexey Boroda   first commit
92
93
94
      public static function fileinputWidget($model, $modelField, $formField = 'fileUpload', $multiple = false) {
          return Yii::$app->artboximage->fileinputWidget($model, $modelField, $formField, $multiple);
      }
9ceae125   Alexey Boroda   -Lazy load (now n...
95
96
97
98
99
100
      
      /**
       * @param $url
       *
       * @return mixed
       */
4ca21c3e   Alexey Boroda   first commit
101
102
103
      private static function getPathFromUrl($url) {
          return substr_replace($url, self::getDriver()->rootPath, 0, strlen(self::getDriver()->rootUrl));
      }
9ceae125   Alexey Boroda   -Lazy load (now n...
104
105
106
107
108
109
      
      /**
       * @param $path
       *
       * @return mixed
       */
4ca21c3e   Alexey Boroda   first commit
110
111
112
      private static function getUrlFromPath($path) {
          return substr_replace($path, self::getDriver()->rootUrl, 0, strlen(self::getDriver()->rootPath));
      }
9ceae125   Alexey Boroda   -Lazy load (now n...
113
114
115
116
117
118
119
120
121
      
      /**
       * Gets path to image with preset
       * @param $filePath
       * @param $preset
       * @param $preset_alias
       *
       * @return bool|mixed
       */
4ca21c3e   Alexey Boroda   first commit
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
      private static function getPresetUrl($filePath, $preset, $preset_alias) {
          $pathinfo = pathinfo($filePath);
          $presetPath = $pathinfo['dirname'] .'/styles/'. strtolower($preset_alias);
          $presetFilePath = $presetPath .'/'. $pathinfo['basename'];
          $presetUrl = self::getUrlFromPath($presetFilePath);
          if (file_exists($presetFilePath)) {
              return $presetUrl;
          }
          if (!file_exists($presetPath)) {
              @mkdir($presetPath, 0777, true);
          }
          $output = self::createPresetImage($filePath, $preset, $preset_alias);
          if ( !empty($output) ) {
              $f = fopen($presetFilePath, 'w');
              fwrite($f, $output);
              fclose($f);
              return $presetUrl;
          }
          return false;
      }
9ceae125   Alexey Boroda   -Lazy load (now n...
142
143
144
145
146
147
148
149
150
      
      /**
       * Create new image from existing one and with selected preset
       * @param $filePath
       * @param $preset
       * @param $preset_alias
       *
       * @return string
       */
4ca21c3e   Alexey Boroda   first commit
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
      private static function createPresetImage($filePath, $preset, $preset_alias)
      {
          $image = self::getDriver()->load($filePath);
          foreach ($preset as $action => $data) {
              switch($action) {
                  case 'resize':
                      $width = empty($data['width']) ? null : $data['width'];
                      $height = empty($data['height']) ? null : $data['height'];
                      $master = empty($data['master']) ? null : $data['master'];
                      $image->resize($width, $height, $master);
                      break;
                  case 'flip':
                      $image->flip(@$data['direction']);
                      break;
                  default:
                      break;
              }
          }
          return $image->render();
      }
  
      /**
       * Get extensions regexp
       * @return string regexp
       */
      private function getExtensionsRegexp()
      {
          $keys = array_keys(self::getDriver()->extensions);
          return '(?i)' . join('|', $keys);
      }
  
      /**
       * Get size from suffix
       * @param string $suffix
       * @return string size
       */
      private function getSizeFromSuffix($suffix)
      {
          return array_search($suffix, $this->getSizeSuffixes());
      }
  
      /**
       * Get suffix from size
       * @param string $size
       * @return string suffix
       */
      private function getSufixFromSize($size)
      {
          return ArrayHelper::getValue($this->getSizeSuffixes(), $size);
      }
  
      private function getSizeSuffixes()
      {
          $suffixes = [];
          foreach ($this->sizes as $size => $sizeConf) {
              $suffixes[$size] = ArrayHelper::getValue($this->sizeSuffixes, $size, $this->defaultSizeSuffix . $size);
          }
          return $suffixes;
      }
  }