|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Zest Framework. |
| 5 | + * |
| 6 | + * @author Muhammad Umer Farooq (Malik) <mumerfarooqlablnet01@gmail.com> |
| 7 | + * |
| 8 | + * @link https://github.com/zestframework/Zest_Framework |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + * @since 3.0.0 |
| 13 | + * |
| 14 | + * @license MIT |
| 15 | + */ |
| 16 | + |
| 17 | +namespace Zest\Image\Avatar; |
| 18 | + |
| 19 | +class ABase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Image binary. |
| 23 | + * |
| 24 | + * @since 3.0.0 |
| 25 | + * |
| 26 | + * @var binary |
| 27 | + */ |
| 28 | + protected $image; |
| 29 | + /** |
| 30 | + * Image foreground color. |
| 31 | + * |
| 32 | + * @since 3.0.0 |
| 33 | + * |
| 34 | + * @var hexa |
| 35 | + */ |
| 36 | + protected $color; |
| 37 | + /** |
| 38 | + * Image background color. |
| 39 | + * |
| 40 | + * @since 3.0.0 |
| 41 | + * |
| 42 | + * @var hexa |
| 43 | + */ |
| 44 | + protected $bgColor; |
| 45 | + /** |
| 46 | + * Image size. |
| 47 | + * |
| 48 | + * @since 3.0.0 |
| 49 | + * |
| 50 | + * @var int |
| 51 | + */ |
| 52 | + protected $size; |
| 53 | + /** |
| 54 | + * Image pixel ratio. |
| 55 | + * |
| 56 | + * @since 3.0.0 |
| 57 | + * |
| 58 | + * @var int |
| 59 | + */ |
| 60 | + protected $pxRatio; |
| 61 | + /** |
| 62 | + * Hash. |
| 63 | + * |
| 64 | + * @since 3.0.0 |
| 65 | + * |
| 66 | + * @var string |
| 67 | + */ |
| 68 | + protected $hash; |
| 69 | + /** |
| 70 | + * Image area of sqare sides. |
| 71 | + * |
| 72 | + * @since 3.0.0 |
| 73 | + * |
| 74 | + * @var array |
| 75 | + */ |
| 76 | + protected $arrayOfSquare; |
| 77 | + /** |
| 78 | + * String. |
| 79 | + * |
| 80 | + * @since 3.0.0 |
| 81 | + * |
| 82 | + * @var string |
| 83 | + */ |
| 84 | + protected $string; |
| 85 | + |
| 86 | + /** |
| 87 | + * Set the image foreground color. |
| 88 | + * |
| 89 | + * @param $color foreground color of image. |
| 90 | + * |
| 91 | + * @since 3.0.0 |
| 92 | + * |
| 93 | + * @return array |
| 94 | + */ |
| 95 | + public function setColor($color) |
| 96 | + { |
| 97 | + if (!empty($color)) { |
| 98 | + $this->color = $this->convertColor($color); |
| 99 | + } |
| 100 | + |
| 101 | + return $this; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Set the image background color. |
| 106 | + * |
| 107 | + * @param $color background color of image. |
| 108 | + * |
| 109 | + * @since 3.0.0 |
| 110 | + * |
| 111 | + * @return array |
| 112 | + */ |
| 113 | + public function setBgColor($color) |
| 114 | + { |
| 115 | + if (!empty($color)) { |
| 116 | + $this->bgColor = $this->convertColor($color); |
| 117 | + } |
| 118 | + |
| 119 | + return $this; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Convert hexa color to rgb or rgba. |
| 124 | + * |
| 125 | + * @param $hex Hexa color code. |
| 126 | + * $alpha (bool) either alpha append or not |
| 127 | + * |
| 128 | + * @since 3.0.0 |
| 129 | + * |
| 130 | + * @return array |
| 131 | + */ |
| 132 | + private function convertColor($hex, $alpha = false) |
| 133 | + { |
| 134 | + $hex = str_replace('#', '', $hex); |
| 135 | + $length = strlen($hex); |
| 136 | + $rgb['0'] = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0)); |
| 137 | + $rgb['1'] = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0)); |
| 138 | + $rgb['2'] = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0)); |
| 139 | + if ($alpha) { |
| 140 | + $rgb['2'] = $alpha; |
| 141 | + } |
| 142 | + |
| 143 | + return $rgb; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get the image foreground color. |
| 148 | + * |
| 149 | + * @since 3.0.0 |
| 150 | + * |
| 151 | + * @return array |
| 152 | + */ |
| 153 | + public function getColor() |
| 154 | + { |
| 155 | + return $this->color; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Get the image background color. |
| 160 | + * |
| 161 | + * @since 3.0.0 |
| 162 | + * |
| 163 | + * @return array |
| 164 | + */ |
| 165 | + public function getBgColor() |
| 166 | + { |
| 167 | + return $this->bgColor; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Convert the hexa to boolean. |
| 172 | + * |
| 173 | + * @param $hexa Hexa number. |
| 174 | + * |
| 175 | + * @since 3.0.0 |
| 176 | + * |
| 177 | + * @return bool |
| 178 | + */ |
| 179 | + private function convertHexaToBool($hexa) |
| 180 | + { |
| 181 | + return (bool) round(hexdec($hexa) / 10); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Convert the hash into multidimessional array. |
| 186 | + * |
| 187 | + * @since 3.0.0 |
| 188 | + * |
| 189 | + * @return array |
| 190 | + */ |
| 191 | + private function convertHashToArrayOfBoolean() |
| 192 | + { |
| 193 | + preg_match_all('/(\d)[\w]/', $this->hash, $matches); |
| 194 | + $this->color = array_map(function ($data) { |
| 195 | + return hexdec($data) * 16; |
| 196 | + }, array_reverse($matches[1])); |
| 197 | + |
| 198 | + return $this; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Get the array of squares. |
| 203 | + * |
| 204 | + * @since 3.0.0 |
| 205 | + * |
| 206 | + * @return array |
| 207 | + */ |
| 208 | + public function getArrayOfSquare() |
| 209 | + { |
| 210 | + return $this->arrayOfSquare; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Get the hash string. |
| 215 | + * |
| 216 | + * @since 3.0.0 |
| 217 | + * |
| 218 | + * @return string |
| 219 | + */ |
| 220 | + public function getHash() |
| 221 | + { |
| 222 | + return $this->hash; |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Set the string. |
| 227 | + * |
| 228 | + * @param $string Text that should be hashed. |
| 229 | + * |
| 230 | + * @since 3.0.0 |
| 231 | + * |
| 232 | + * @return mix |
| 233 | + */ |
| 234 | + public function setHashString(string $string) |
| 235 | + { |
| 236 | + if (!empty(trim($string))) { |
| 237 | + $this->string = $string; |
| 238 | + $this->hash = md5($string); |
| 239 | + $this->convertHashToArrayOfBoolean(); |
| 240 | + |
| 241 | + return $this; |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Get the character. |
| 247 | + * |
| 248 | + * @since 3.0.0 |
| 249 | + * |
| 250 | + * @return char |
| 251 | + */ |
| 252 | + public function getCharacter() |
| 253 | + { |
| 254 | + return strtoupper($this->string[0]); |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Set size of image. |
| 259 | + * |
| 260 | + * @param $size size of image. |
| 261 | + * |
| 262 | + * @since 3.0.0 |
| 263 | + * |
| 264 | + * @return int |
| 265 | + */ |
| 266 | + public function setSize($size) |
| 267 | + { |
| 268 | + if (!empty($size)) { |
| 269 | + $this->size = $size; |
| 270 | + $this->pxRatio = round($size / 5); |
| 271 | + |
| 272 | + return $this; |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Get the size of image. |
| 278 | + * |
| 279 | + * @since 3.0.0 |
| 280 | + * |
| 281 | + * @return int |
| 282 | + */ |
| 283 | + public function getSize() |
| 284 | + { |
| 285 | + return $this->size; |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * Get the pixel ratio. |
| 290 | + * |
| 291 | + * @since 3.0.0 |
| 292 | + * |
| 293 | + * @return int |
| 294 | + */ |
| 295 | + public function getPxRatio() |
| 296 | + { |
| 297 | + return $this->pxRatio; |
| 298 | + } |
| 299 | +} |
0 commit comments