Blame view

frontend/components/ImageResizer.php 5.53 KB
d1f8bd40   Alexey Boroda   first commit
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
139
140
141
142
143
144
145
146
147
148
149
150
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
  <?php
  
  namespace frontend\components;
  
  use Yii;
  use yii\imagine\Image;
  use Imagine\Image\Point;
  use Imagine\Image\ManipulatorInterface;
  
  /**
   *
   * @author FilamentV <vortex.filament@gmail.com>
   * @copyright (c), Thread
   */
  class ImageResizer
  {
      const BASE_PATH_TO_CACHE = '@uploads/thumbs';
      const BASE_URL_TO_CACHE = '/uploads/thumbs/';
  
      /**
       * @param $image
       * @param $width
       * @param $height
       * @param string $prefix
       * @return string
       */
      public static function generateName($image, $width, $height, $prefix = 'thumb{$width}_{$height}.')
      {
          return str_replace(['{$width}', '{$height}'], [$width, $height], $prefix) . basename($image);
      }
  
      /**
       * @param $image
       * @param $width
       * @param $height
       * @param string $prefix
       * @return null|string
       */
      public static function getThumb($image, $width, $height, $prefix = 'thumb{$width}_{$height}.')
      {
          if (is_file($image)) {
              $base = Yii::getAlias(self::BASE_PATH_TO_CACHE) . '/';
              $name = self::generateName($image, $width, $height, $prefix);
  
              if (is_file($base . $name)) {
                  return self::BASE_URL_TO_CACHE . $name;
              }
              $image = Image::thumbnail($image, $width, $height, ManipulatorInterface::THUMBNAIL_INSET);
              if ($image->save($base . $name, [
                  'jpeg_quality' => 90,
              ])
              ) {
                  return self::BASE_URL_TO_CACHE . $name;
              }
          }
          return null;
      }
  
      /**
       * @param $image
       * @param $watermark
       * @param $color
       * @param $prefix
       * @return null|string
       */
      public static function getSign($image, $watermark, $color, $prefix)
      {
          if (is_file($image) && is_file($watermark) && !empty($color)) {
              $base = Yii::getAlias(self::BASE_PATH_TO_CACHE) . '/';
              $name = basename($image);
              if (is_file($base . $prefix . $name)) {
                  return self::BASE_URL_TO_CACHE . $prefix . $name;
              }
              self::changeColor($image, $base . $prefix . $name, $color);
              self::setWatermark($base . $prefix . $name, $base . $prefix . $name, $watermark);
  
              return self::BASE_URL_TO_CACHE . $prefix . $name;
          } else {
  //            echo 'no';
          }
          return null;
      }
  
      /**
       * @param $source
       * @param $destination
       * @param $watermark
       */
      public static function setWatermark($source, $destination, $watermark)
      {
          if (is_file($source) && is_file($watermark)) {
  
              $watermark = Image::getImagine()->open($watermark);
              $image = Image::getImagine()->open($source);
              $size = $image->getSize();
              $wSize = $watermark->getSize();
              //
              $image->paste($watermark, new Point(($size->getWidth() - $wSize->getWidth()) / 2, ($size->getHeight() - $wSize->getHeight()) / 2));
              //
              $image->save($destination);
          }
      }
  
      /**
       * @param $source
       * @param $destination
       * @param $color
       * @return null|string
       */
      public static function changeColor($source, $destination, $color)
      {
  //        echo $color;
          if (is_file($source)) {
              /* Convert hexdec color string to rgb(a) string */
  
              /**
               * @param $image
               * @param $color
               */
  
  
              $im = imagecreatefrompng($source);
              imagealphablending($im, true); // setting alpha blending on
              imagesavealpha($im, true); // save alphablending setting (important)
              static::imagefon($im, $color);
              imagepng($im, $destination);
  
              return $destination;
          }
          return null;
      }
  
      /**
       * @param $image
       * @param $color
       */
      public static function imagefon(&$image, $color)
      {
          $c = ImageResizer::hex2rgba($color);
          $width = imagesx($image);
          $height = imagesy($image);
  //        $gray = imagecolorat($image, 0, 0);
  
          for ($x = 0; $x < $width; $x++) {
              for ($y = 0; $y < $height; $y++) {
                  $rgb = imagecolorat($image, $x, $y);
  
                  if (16777215 == $rgb) {
  //                            imagesetpixel($image, $x, $y, 19997000);
                      imagesetpixel($image, $x, $y, imagecolorallocate($image, $c[0], $c[1], $c[2]));
                  }
  //                                imagesetpixel($image, $x, $y, imagecolorallocate($image, 0, 0, 0));
  //                            } else {
  //                                imagesetpixel($image, $x, $y, imagecolortransparent($image, imagecolorallocate($image, 0, 0, 0)));
  //                            }
              }
          }
      }
  
      /**
       * @param $color
       * @return array
       */
      public static function hex2rgba($color)
      {
          //
          $default = [0, 0, 0];
          //Return default if no color provided
          if (empty($color)) {
              return $default;
          }
          //Sanitize $color if "#" is provided
          if ($color[0] == '#') {
              $color = substr($color, 1);
          }
          //Check if color has 6 or 3 characters and get values
          if (strlen($color) == 6) {
              $hex = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
          } elseif (strlen($color) == 3) {
              $hex = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
          } else {
              return $default;
          }
          //Convert hexadec to rgb
          $rgb = array_map('hexdec', $hex);
          //
          return $rgb;
      }
  }