@@ -44,6 +44,12 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | class Color { |
| 46 | 46 | public $r, $g, $b; |
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param integer $r |
|
| 50 | + * @param integer $g |
|
| 51 | + * @param integer $b |
|
| 52 | + */ |
|
| 47 | 53 | public function __construct($r, $g, $b) { |
| 48 | 54 | $this->r = $r; |
| 49 | 55 | $this->g = $g; |
@@ -304,6 +310,7 @@ discard block |
||
| 304 | 310 | * Calculate steps between two Colors |
| 305 | 311 | * @param object Color $steps start color |
| 306 | 312 | * @param object Color $ends end color |
| 313 | + * @param integer $steps |
|
| 307 | 314 | * @return array [r,g,b] steps for each color to go from $steps to $ends |
| 308 | 315 | */ |
| 309 | 316 | private function stepCalc($steps, $ends) { |
@@ -315,8 +322,9 @@ discard block |
||
| 315 | 322 | } |
| 316 | 323 | /** |
| 317 | 324 | * Convert a string to an integer evenly |
| 318 | - * @param string $hash the text to parse |
|
| 319 | - * @param int $maximum the maximum range |
|
| 325 | + * @param integer $steps |
|
| 326 | + * @param Color $color1 |
|
| 327 | + * @param Color $color2 |
|
| 320 | 328 | * @return int between 0 and $maximum |
| 321 | 329 | */ |
| 322 | 330 | private function mixPalette($steps, $color1, $color2) { |
@@ -43,12 +43,12 @@ discard block |
||
| 43 | 43 | use OCP\ILogger; |
| 44 | 44 | |
| 45 | 45 | class Color { |
| 46 | - public $r, $g, $b; |
|
| 47 | - public function __construct($r, $g, $b) { |
|
| 48 | - $this->r = $r; |
|
| 49 | - $this->g = $g; |
|
| 50 | - $this->b = $b; |
|
| 51 | - } |
|
| 46 | + public $r, $g, $b; |
|
| 47 | + public function __construct($r, $g, $b) { |
|
| 48 | + $this->r = $r; |
|
| 49 | + $this->g = $g; |
|
| 50 | + $this->b = $b; |
|
| 51 | + } |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
@@ -56,345 +56,345 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | |
| 58 | 58 | class Avatar implements IAvatar { |
| 59 | - /** @var ISimpleFolder */ |
|
| 60 | - private $folder; |
|
| 61 | - /** @var IL10N */ |
|
| 62 | - private $l; |
|
| 63 | - /** @var User */ |
|
| 64 | - private $user; |
|
| 65 | - /** @var ILogger */ |
|
| 66 | - private $logger; |
|
| 67 | - /** @var IConfig */ |
|
| 68 | - private $config; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * constructor |
|
| 72 | - * |
|
| 73 | - * @param ISimpleFolder $folder The folder where the avatars are |
|
| 74 | - * @param IL10N $l |
|
| 75 | - * @param User $user |
|
| 76 | - * @param ILogger $logger |
|
| 77 | - * @param IConfig $config |
|
| 78 | - */ |
|
| 79 | - public function __construct(ISimpleFolder $folder, |
|
| 80 | - IL10N $l, |
|
| 81 | - $user, |
|
| 82 | - ILogger $logger, |
|
| 83 | - IConfig $config) { |
|
| 84 | - $this->folder = $folder; |
|
| 85 | - $this->l = $l; |
|
| 86 | - $this->user = $user; |
|
| 87 | - $this->logger = $logger; |
|
| 88 | - $this->config = $config; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @inheritdoc |
|
| 93 | - */ |
|
| 94 | - public function get ($size = 64) { |
|
| 95 | - try { |
|
| 96 | - $file = $this->getFile($size); |
|
| 97 | - } catch (NotFoundException $e) { |
|
| 98 | - return false; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $avatar = new OC_Image(); |
|
| 102 | - $avatar->loadFromData($file->getContent()); |
|
| 103 | - return $avatar; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Check if an avatar exists for the user |
|
| 108 | - * |
|
| 109 | - * @return bool |
|
| 110 | - */ |
|
| 111 | - public function exists() { |
|
| 112 | - |
|
| 113 | - return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * sets the users avatar |
|
| 118 | - * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
| 119 | - * @throws \Exception if the provided file is not a jpg or png image |
|
| 120 | - * @throws \Exception if the provided image is not valid |
|
| 121 | - * @throws NotSquareException if the image is not square |
|
| 122 | - * @return void |
|
| 123 | - */ |
|
| 124 | - public function set ($data) { |
|
| 125 | - |
|
| 126 | - if($data instanceOf IImage) { |
|
| 127 | - $img = $data; |
|
| 128 | - $data = $img->data(); |
|
| 129 | - } else { |
|
| 130 | - $img = new OC_Image(); |
|
| 131 | - if (is_resource($data) && get_resource_type($data) === "gd") { |
|
| 132 | - $img->setResource($data); |
|
| 133 | - } elseif(is_resource($data)) { |
|
| 134 | - $img->loadFromFileHandle($data); |
|
| 135 | - } else { |
|
| 136 | - try { |
|
| 137 | - // detect if it is a path or maybe the images as string |
|
| 138 | - $result = @realpath($data); |
|
| 139 | - if ($result === false || $result === null) { |
|
| 140 | - $img->loadFromData($data); |
|
| 141 | - } else { |
|
| 142 | - $img->loadFromFile($data); |
|
| 143 | - } |
|
| 144 | - } catch (\Error $e) { |
|
| 145 | - $img->loadFromData($data); |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - $type = substr($img->mimeType(), -3); |
|
| 150 | - if ($type === 'peg') { |
|
| 151 | - $type = 'jpg'; |
|
| 152 | - } |
|
| 153 | - if ($type !== 'jpg' && $type !== 'png') { |
|
| 154 | - throw new \Exception($this->l->t('Unknown filetype')); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if (!$img->valid()) { |
|
| 158 | - throw new \Exception($this->l->t('Invalid image')); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if (!($img->height() === $img->width())) { |
|
| 162 | - throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - $this->remove(); |
|
| 166 | - $file = $this->folder->newFile('avatar.'.$type); |
|
| 167 | - $file->putContent($data); |
|
| 168 | - |
|
| 169 | - try { |
|
| 170 | - $generated = $this->folder->getFile('generated'); |
|
| 171 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'false'); |
|
| 172 | - $generated->delete(); |
|
| 173 | - } catch (NotFoundException $e) { |
|
| 174 | - // |
|
| 175 | - } |
|
| 176 | - $this->user->triggerChange('avatar', $file); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * remove the users avatar |
|
| 181 | - * @return void |
|
| 182 | - */ |
|
| 183 | - public function remove () { |
|
| 184 | - $avatars = $this->folder->getDirectoryListing(); |
|
| 185 | - |
|
| 186 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
| 187 | - (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 188 | - |
|
| 189 | - foreach ($avatars as $avatar) { |
|
| 190 | - $avatar->delete(); |
|
| 191 | - } |
|
| 192 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 193 | - $this->user->triggerChange('avatar', ''); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * @inheritdoc |
|
| 198 | - */ |
|
| 199 | - public function getFile($size) { |
|
| 200 | - try { |
|
| 201 | - $ext = $this->getExtension(); |
|
| 202 | - } catch (NotFoundException $e) { |
|
| 203 | - $data = $this->generateAvatar($this->user->getDisplayName(), 1024); |
|
| 204 | - $avatar = $this->folder->newFile('avatar.png'); |
|
| 205 | - $avatar->putContent($data); |
|
| 206 | - $ext = 'png'; |
|
| 207 | - |
|
| 208 | - $this->folder->newFile('generated'); |
|
| 209 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - if ($size === -1) { |
|
| 213 | - $path = 'avatar.' . $ext; |
|
| 214 | - } else { |
|
| 215 | - $path = 'avatar.' . $size . '.' . $ext; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - try { |
|
| 219 | - $file = $this->folder->getFile($path); |
|
| 220 | - } catch (NotFoundException $e) { |
|
| 221 | - if ($size <= 0) { |
|
| 222 | - throw new NotFoundException; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - if ($this->folder->fileExists('generated')) { |
|
| 226 | - $data = $this->generateAvatar($this->user->getDisplayName(), $size); |
|
| 227 | - |
|
| 228 | - } else { |
|
| 229 | - $avatar = new OC_Image(); |
|
| 230 | - /** @var ISimpleFile $file */ |
|
| 231 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
| 232 | - $avatar->loadFromData($file->getContent()); |
|
| 233 | - $avatar->resize($size); |
|
| 234 | - $data = $avatar->data(); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - try { |
|
| 238 | - $file = $this->folder->newFile($path); |
|
| 239 | - $file->putContent($data); |
|
| 240 | - } catch (NotPermittedException $e) { |
|
| 241 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 242 | - throw new NotFoundException(); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - if($this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', null) === null) { |
|
| 248 | - $generated = $this->folder->fileExists('generated') ? 'true' : 'false'; |
|
| 249 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', $generated); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return $file; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Get the extension of the avatar. If there is no avatar throw Exception |
|
| 257 | - * |
|
| 258 | - * @return string |
|
| 259 | - * @throws NotFoundException |
|
| 260 | - */ |
|
| 261 | - private function getExtension() { |
|
| 262 | - if ($this->folder->fileExists('avatar.jpg')) { |
|
| 263 | - return 'jpg'; |
|
| 264 | - } elseif ($this->folder->fileExists('avatar.png')) { |
|
| 265 | - return 'png'; |
|
| 266 | - } |
|
| 267 | - throw new NotFoundException; |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * @param string $userDisplayName |
|
| 272 | - * @param int $size |
|
| 273 | - * @return string |
|
| 274 | - */ |
|
| 275 | - private function generateAvatar($userDisplayName, $size) { |
|
| 276 | - $text = mb_strtoupper(mb_substr($userDisplayName, 0, 1), 'UTF-8'); |
|
| 277 | - $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
| 278 | - |
|
| 279 | - $im = imagecreatetruecolor($size, $size); |
|
| 280 | - $background = imagecolorallocate($im, $backgroundColor->r, $backgroundColor->g, $backgroundColor->b); |
|
| 281 | - $white = imagecolorallocate($im, 255, 255, 255); |
|
| 282 | - imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
| 283 | - |
|
| 284 | - $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.ttf'; |
|
| 285 | - |
|
| 286 | - $fontSize = $size * 0.4; |
|
| 287 | - $box = imagettfbbox($fontSize, 0, $font, $text); |
|
| 288 | - |
|
| 289 | - $x = ($size - ($box[2] - $box[0])) / 2; |
|
| 290 | - $y = ($size - ($box[1] - $box[7])) / 2; |
|
| 291 | - $x += 1; |
|
| 292 | - $y -= $box[7]; |
|
| 293 | - imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
|
| 294 | - |
|
| 295 | - ob_start(); |
|
| 296 | - imagepng($im); |
|
| 297 | - $data = ob_get_contents(); |
|
| 298 | - ob_end_clean(); |
|
| 299 | - |
|
| 300 | - return $data; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Calculate steps between two Colors |
|
| 305 | - * @param object Color $steps start color |
|
| 306 | - * @param object Color $ends end color |
|
| 307 | - * @return array [r,g,b] steps for each color to go from $steps to $ends |
|
| 308 | - */ |
|
| 309 | - private function stepCalc($steps, $ends) { |
|
| 310 | - $step = array(); |
|
| 311 | - $step[0] = ($ends[1]->r - $ends[0]->r) / $steps; |
|
| 312 | - $step[1] = ($ends[1]->g - $ends[0]->g) / $steps; |
|
| 313 | - $step[2] = ($ends[1]->b - $ends[0]->b) / $steps; |
|
| 314 | - return $step; |
|
| 315 | - } |
|
| 316 | - /** |
|
| 317 | - * Convert a string to an integer evenly |
|
| 318 | - * @param string $hash the text to parse |
|
| 319 | - * @param int $maximum the maximum range |
|
| 320 | - * @return int between 0 and $maximum |
|
| 321 | - */ |
|
| 322 | - private function mixPalette($steps, $color1, $color2) { |
|
| 323 | - $count = $steps + 1; |
|
| 324 | - $palette = array($color1); |
|
| 325 | - $step = $this->stepCalc($steps, [$color1, $color2]); |
|
| 326 | - for ($i = 1; $i < $steps; $i++) { |
|
| 327 | - $r = intval($color1->r + ($step[0] * $i)); |
|
| 328 | - $g = intval($color1->g + ($step[1] * $i)); |
|
| 329 | - $b = intval($color1->b + ($step[2] * $i)); |
|
| 330 | - $palette[] = new Color($r, $g, $b); |
|
| 331 | - } |
|
| 332 | - return $palette; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Convert a string to an integer evenly |
|
| 338 | - * @param string $hash the text to parse |
|
| 339 | - * @param int $maximum the maximum range |
|
| 340 | - * @return int between 0 and $maximum |
|
| 341 | - */ |
|
| 342 | - private function hashToInt($hash, $maximum) { |
|
| 343 | - $final = 0; |
|
| 344 | - $result = array(); |
|
| 345 | - |
|
| 346 | - // Splitting evenly the string |
|
| 347 | - for ($i=0; $i< strlen($hash); $i++) { |
|
| 348 | - // chars in md5 goes up to f, hex:16 |
|
| 349 | - $result[] = intval(substr($hash, $i, 1), 16) % 16; |
|
| 350 | - } |
|
| 351 | - // Adds up all results |
|
| 352 | - foreach ($result as $value) { |
|
| 353 | - $final += $value; |
|
| 354 | - } |
|
| 355 | - // chars in md5 goes up to f, hex:16 |
|
| 356 | - return intval($final % $maximum); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * @param string $text |
|
| 362 | - * @return Color Object containting r g b int in the range [0, 255] |
|
| 363 | - */ |
|
| 364 | - function avatarBackgroundColor($text) { |
|
| 365 | - $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
|
| 366 | - |
|
| 367 | - $hash = md5($hash); |
|
| 368 | - $hashChars = str_split($hash); |
|
| 369 | - |
|
| 370 | - $red = new Color(182, 70, 157); |
|
| 371 | - $yellow = new Color(221, 203, 85); |
|
| 372 | - $blue = new Color(0, 130, 201); // Nextcloud blue |
|
| 373 | - // Number of steps to go from a color to another |
|
| 374 | - // 3 colors * 6 will result in 18 generated colors |
|
| 375 | - $steps = 6; |
|
| 376 | - |
|
| 377 | - $palette1 = $this->mixPalette($steps, $red, $yellow); |
|
| 378 | - $palette2 = $this->mixPalette($steps, $yellow, $blue); |
|
| 379 | - $palette3 = $this->mixPalette($steps, $blue, $red); |
|
| 380 | - |
|
| 381 | - $finalPalette = array_merge($palette1, $palette2, $palette3); |
|
| 382 | - |
|
| 383 | - return $finalPalette[$this->hashToInt($hash, $steps * 3 )]; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - public function userChanged($feature, $oldValue, $newValue) { |
|
| 387 | - // We only change the avatar on display name changes |
|
| 388 | - if ($feature !== 'displayName') { |
|
| 389 | - return; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - // If the avatar is not generated (so an uploaded image) we skip this |
|
| 393 | - if (!$this->folder->fileExists('generated')) { |
|
| 394 | - return; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - $this->remove(); |
|
| 398 | - } |
|
| 59 | + /** @var ISimpleFolder */ |
|
| 60 | + private $folder; |
|
| 61 | + /** @var IL10N */ |
|
| 62 | + private $l; |
|
| 63 | + /** @var User */ |
|
| 64 | + private $user; |
|
| 65 | + /** @var ILogger */ |
|
| 66 | + private $logger; |
|
| 67 | + /** @var IConfig */ |
|
| 68 | + private $config; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * constructor |
|
| 72 | + * |
|
| 73 | + * @param ISimpleFolder $folder The folder where the avatars are |
|
| 74 | + * @param IL10N $l |
|
| 75 | + * @param User $user |
|
| 76 | + * @param ILogger $logger |
|
| 77 | + * @param IConfig $config |
|
| 78 | + */ |
|
| 79 | + public function __construct(ISimpleFolder $folder, |
|
| 80 | + IL10N $l, |
|
| 81 | + $user, |
|
| 82 | + ILogger $logger, |
|
| 83 | + IConfig $config) { |
|
| 84 | + $this->folder = $folder; |
|
| 85 | + $this->l = $l; |
|
| 86 | + $this->user = $user; |
|
| 87 | + $this->logger = $logger; |
|
| 88 | + $this->config = $config; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @inheritdoc |
|
| 93 | + */ |
|
| 94 | + public function get ($size = 64) { |
|
| 95 | + try { |
|
| 96 | + $file = $this->getFile($size); |
|
| 97 | + } catch (NotFoundException $e) { |
|
| 98 | + return false; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $avatar = new OC_Image(); |
|
| 102 | + $avatar->loadFromData($file->getContent()); |
|
| 103 | + return $avatar; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Check if an avatar exists for the user |
|
| 108 | + * |
|
| 109 | + * @return bool |
|
| 110 | + */ |
|
| 111 | + public function exists() { |
|
| 112 | + |
|
| 113 | + return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * sets the users avatar |
|
| 118 | + * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
| 119 | + * @throws \Exception if the provided file is not a jpg or png image |
|
| 120 | + * @throws \Exception if the provided image is not valid |
|
| 121 | + * @throws NotSquareException if the image is not square |
|
| 122 | + * @return void |
|
| 123 | + */ |
|
| 124 | + public function set ($data) { |
|
| 125 | + |
|
| 126 | + if($data instanceOf IImage) { |
|
| 127 | + $img = $data; |
|
| 128 | + $data = $img->data(); |
|
| 129 | + } else { |
|
| 130 | + $img = new OC_Image(); |
|
| 131 | + if (is_resource($data) && get_resource_type($data) === "gd") { |
|
| 132 | + $img->setResource($data); |
|
| 133 | + } elseif(is_resource($data)) { |
|
| 134 | + $img->loadFromFileHandle($data); |
|
| 135 | + } else { |
|
| 136 | + try { |
|
| 137 | + // detect if it is a path or maybe the images as string |
|
| 138 | + $result = @realpath($data); |
|
| 139 | + if ($result === false || $result === null) { |
|
| 140 | + $img->loadFromData($data); |
|
| 141 | + } else { |
|
| 142 | + $img->loadFromFile($data); |
|
| 143 | + } |
|
| 144 | + } catch (\Error $e) { |
|
| 145 | + $img->loadFromData($data); |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + $type = substr($img->mimeType(), -3); |
|
| 150 | + if ($type === 'peg') { |
|
| 151 | + $type = 'jpg'; |
|
| 152 | + } |
|
| 153 | + if ($type !== 'jpg' && $type !== 'png') { |
|
| 154 | + throw new \Exception($this->l->t('Unknown filetype')); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if (!$img->valid()) { |
|
| 158 | + throw new \Exception($this->l->t('Invalid image')); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if (!($img->height() === $img->width())) { |
|
| 162 | + throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + $this->remove(); |
|
| 166 | + $file = $this->folder->newFile('avatar.'.$type); |
|
| 167 | + $file->putContent($data); |
|
| 168 | + |
|
| 169 | + try { |
|
| 170 | + $generated = $this->folder->getFile('generated'); |
|
| 171 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'false'); |
|
| 172 | + $generated->delete(); |
|
| 173 | + } catch (NotFoundException $e) { |
|
| 174 | + // |
|
| 175 | + } |
|
| 176 | + $this->user->triggerChange('avatar', $file); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * remove the users avatar |
|
| 181 | + * @return void |
|
| 182 | + */ |
|
| 183 | + public function remove () { |
|
| 184 | + $avatars = $this->folder->getDirectoryListing(); |
|
| 185 | + |
|
| 186 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
| 187 | + (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 188 | + |
|
| 189 | + foreach ($avatars as $avatar) { |
|
| 190 | + $avatar->delete(); |
|
| 191 | + } |
|
| 192 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 193 | + $this->user->triggerChange('avatar', ''); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * @inheritdoc |
|
| 198 | + */ |
|
| 199 | + public function getFile($size) { |
|
| 200 | + try { |
|
| 201 | + $ext = $this->getExtension(); |
|
| 202 | + } catch (NotFoundException $e) { |
|
| 203 | + $data = $this->generateAvatar($this->user->getDisplayName(), 1024); |
|
| 204 | + $avatar = $this->folder->newFile('avatar.png'); |
|
| 205 | + $avatar->putContent($data); |
|
| 206 | + $ext = 'png'; |
|
| 207 | + |
|
| 208 | + $this->folder->newFile('generated'); |
|
| 209 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + if ($size === -1) { |
|
| 213 | + $path = 'avatar.' . $ext; |
|
| 214 | + } else { |
|
| 215 | + $path = 'avatar.' . $size . '.' . $ext; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + try { |
|
| 219 | + $file = $this->folder->getFile($path); |
|
| 220 | + } catch (NotFoundException $e) { |
|
| 221 | + if ($size <= 0) { |
|
| 222 | + throw new NotFoundException; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + if ($this->folder->fileExists('generated')) { |
|
| 226 | + $data = $this->generateAvatar($this->user->getDisplayName(), $size); |
|
| 227 | + |
|
| 228 | + } else { |
|
| 229 | + $avatar = new OC_Image(); |
|
| 230 | + /** @var ISimpleFile $file */ |
|
| 231 | + $file = $this->folder->getFile('avatar.' . $ext); |
|
| 232 | + $avatar->loadFromData($file->getContent()); |
|
| 233 | + $avatar->resize($size); |
|
| 234 | + $data = $avatar->data(); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + try { |
|
| 238 | + $file = $this->folder->newFile($path); |
|
| 239 | + $file->putContent($data); |
|
| 240 | + } catch (NotPermittedException $e) { |
|
| 241 | + $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 242 | + throw new NotFoundException(); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + if($this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', null) === null) { |
|
| 248 | + $generated = $this->folder->fileExists('generated') ? 'true' : 'false'; |
|
| 249 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', $generated); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return $file; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Get the extension of the avatar. If there is no avatar throw Exception |
|
| 257 | + * |
|
| 258 | + * @return string |
|
| 259 | + * @throws NotFoundException |
|
| 260 | + */ |
|
| 261 | + private function getExtension() { |
|
| 262 | + if ($this->folder->fileExists('avatar.jpg')) { |
|
| 263 | + return 'jpg'; |
|
| 264 | + } elseif ($this->folder->fileExists('avatar.png')) { |
|
| 265 | + return 'png'; |
|
| 266 | + } |
|
| 267 | + throw new NotFoundException; |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * @param string $userDisplayName |
|
| 272 | + * @param int $size |
|
| 273 | + * @return string |
|
| 274 | + */ |
|
| 275 | + private function generateAvatar($userDisplayName, $size) { |
|
| 276 | + $text = mb_strtoupper(mb_substr($userDisplayName, 0, 1), 'UTF-8'); |
|
| 277 | + $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
| 278 | + |
|
| 279 | + $im = imagecreatetruecolor($size, $size); |
|
| 280 | + $background = imagecolorallocate($im, $backgroundColor->r, $backgroundColor->g, $backgroundColor->b); |
|
| 281 | + $white = imagecolorallocate($im, 255, 255, 255); |
|
| 282 | + imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
| 283 | + |
|
| 284 | + $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.ttf'; |
|
| 285 | + |
|
| 286 | + $fontSize = $size * 0.4; |
|
| 287 | + $box = imagettfbbox($fontSize, 0, $font, $text); |
|
| 288 | + |
|
| 289 | + $x = ($size - ($box[2] - $box[0])) / 2; |
|
| 290 | + $y = ($size - ($box[1] - $box[7])) / 2; |
|
| 291 | + $x += 1; |
|
| 292 | + $y -= $box[7]; |
|
| 293 | + imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
|
| 294 | + |
|
| 295 | + ob_start(); |
|
| 296 | + imagepng($im); |
|
| 297 | + $data = ob_get_contents(); |
|
| 298 | + ob_end_clean(); |
|
| 299 | + |
|
| 300 | + return $data; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Calculate steps between two Colors |
|
| 305 | + * @param object Color $steps start color |
|
| 306 | + * @param object Color $ends end color |
|
| 307 | + * @return array [r,g,b] steps for each color to go from $steps to $ends |
|
| 308 | + */ |
|
| 309 | + private function stepCalc($steps, $ends) { |
|
| 310 | + $step = array(); |
|
| 311 | + $step[0] = ($ends[1]->r - $ends[0]->r) / $steps; |
|
| 312 | + $step[1] = ($ends[1]->g - $ends[0]->g) / $steps; |
|
| 313 | + $step[2] = ($ends[1]->b - $ends[0]->b) / $steps; |
|
| 314 | + return $step; |
|
| 315 | + } |
|
| 316 | + /** |
|
| 317 | + * Convert a string to an integer evenly |
|
| 318 | + * @param string $hash the text to parse |
|
| 319 | + * @param int $maximum the maximum range |
|
| 320 | + * @return int between 0 and $maximum |
|
| 321 | + */ |
|
| 322 | + private function mixPalette($steps, $color1, $color2) { |
|
| 323 | + $count = $steps + 1; |
|
| 324 | + $palette = array($color1); |
|
| 325 | + $step = $this->stepCalc($steps, [$color1, $color2]); |
|
| 326 | + for ($i = 1; $i < $steps; $i++) { |
|
| 327 | + $r = intval($color1->r + ($step[0] * $i)); |
|
| 328 | + $g = intval($color1->g + ($step[1] * $i)); |
|
| 329 | + $b = intval($color1->b + ($step[2] * $i)); |
|
| 330 | + $palette[] = new Color($r, $g, $b); |
|
| 331 | + } |
|
| 332 | + return $palette; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Convert a string to an integer evenly |
|
| 338 | + * @param string $hash the text to parse |
|
| 339 | + * @param int $maximum the maximum range |
|
| 340 | + * @return int between 0 and $maximum |
|
| 341 | + */ |
|
| 342 | + private function hashToInt($hash, $maximum) { |
|
| 343 | + $final = 0; |
|
| 344 | + $result = array(); |
|
| 345 | + |
|
| 346 | + // Splitting evenly the string |
|
| 347 | + for ($i=0; $i< strlen($hash); $i++) { |
|
| 348 | + // chars in md5 goes up to f, hex:16 |
|
| 349 | + $result[] = intval(substr($hash, $i, 1), 16) % 16; |
|
| 350 | + } |
|
| 351 | + // Adds up all results |
|
| 352 | + foreach ($result as $value) { |
|
| 353 | + $final += $value; |
|
| 354 | + } |
|
| 355 | + // chars in md5 goes up to f, hex:16 |
|
| 356 | + return intval($final % $maximum); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * @param string $text |
|
| 362 | + * @return Color Object containting r g b int in the range [0, 255] |
|
| 363 | + */ |
|
| 364 | + function avatarBackgroundColor($text) { |
|
| 365 | + $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
|
| 366 | + |
|
| 367 | + $hash = md5($hash); |
|
| 368 | + $hashChars = str_split($hash); |
|
| 369 | + |
|
| 370 | + $red = new Color(182, 70, 157); |
|
| 371 | + $yellow = new Color(221, 203, 85); |
|
| 372 | + $blue = new Color(0, 130, 201); // Nextcloud blue |
|
| 373 | + // Number of steps to go from a color to another |
|
| 374 | + // 3 colors * 6 will result in 18 generated colors |
|
| 375 | + $steps = 6; |
|
| 376 | + |
|
| 377 | + $palette1 = $this->mixPalette($steps, $red, $yellow); |
|
| 378 | + $palette2 = $this->mixPalette($steps, $yellow, $blue); |
|
| 379 | + $palette3 = $this->mixPalette($steps, $blue, $red); |
|
| 380 | + |
|
| 381 | + $finalPalette = array_merge($palette1, $palette2, $palette3); |
|
| 382 | + |
|
| 383 | + return $finalPalette[$this->hashToInt($hash, $steps * 3 )]; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + public function userChanged($feature, $oldValue, $newValue) { |
|
| 387 | + // We only change the avatar on display name changes |
|
| 388 | + if ($feature !== 'displayName') { |
|
| 389 | + return; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + // If the avatar is not generated (so an uploaded image) we skip this |
|
| 393 | + if (!$this->folder->fileExists('generated')) { |
|
| 394 | + return; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + $this->remove(); |
|
| 398 | + } |
|
| 399 | 399 | |
| 400 | 400 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | /** |
| 92 | 92 | * @inheritdoc |
| 93 | 93 | */ |
| 94 | - public function get ($size = 64) { |
|
| 94 | + public function get($size = 64) { |
|
| 95 | 95 | try { |
| 96 | 96 | $file = $this->getFile($size); |
| 97 | 97 | } catch (NotFoundException $e) { |
@@ -121,16 +121,16 @@ discard block |
||
| 121 | 121 | * @throws NotSquareException if the image is not square |
| 122 | 122 | * @return void |
| 123 | 123 | */ |
| 124 | - public function set ($data) { |
|
| 124 | + public function set($data) { |
|
| 125 | 125 | |
| 126 | - if($data instanceOf IImage) { |
|
| 126 | + if ($data instanceOf IImage) { |
|
| 127 | 127 | $img = $data; |
| 128 | 128 | $data = $img->data(); |
| 129 | 129 | } else { |
| 130 | 130 | $img = new OC_Image(); |
| 131 | 131 | if (is_resource($data) && get_resource_type($data) === "gd") { |
| 132 | 132 | $img->setResource($data); |
| 133 | - } elseif(is_resource($data)) { |
|
| 133 | + } elseif (is_resource($data)) { |
|
| 134 | 134 | $img->loadFromFileHandle($data); |
| 135 | 135 | } else { |
| 136 | 136 | try { |
@@ -180,11 +180,11 @@ discard block |
||
| 180 | 180 | * remove the users avatar |
| 181 | 181 | * @return void |
| 182 | 182 | */ |
| 183 | - public function remove () { |
|
| 183 | + public function remove() { |
|
| 184 | 184 | $avatars = $this->folder->getDirectoryListing(); |
| 185 | 185 | |
| 186 | 186 | $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
| 187 | - (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 187 | + (int) $this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 188 | 188 | |
| 189 | 189 | foreach ($avatars as $avatar) { |
| 190 | 190 | $avatar->delete(); |
@@ -210,9 +210,9 @@ discard block |
||
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | if ($size === -1) { |
| 213 | - $path = 'avatar.' . $ext; |
|
| 213 | + $path = 'avatar.'.$ext; |
|
| 214 | 214 | } else { |
| 215 | - $path = 'avatar.' . $size . '.' . $ext; |
|
| 215 | + $path = 'avatar.'.$size.'.'.$ext; |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | try { |
@@ -228,7 +228,7 @@ discard block |
||
| 228 | 228 | } else { |
| 229 | 229 | $avatar = new OC_Image(); |
| 230 | 230 | /** @var ISimpleFile $file */ |
| 231 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
| 231 | + $file = $this->folder->getFile('avatar.'.$ext); |
|
| 232 | 232 | $avatar->loadFromData($file->getContent()); |
| 233 | 233 | $avatar->resize($size); |
| 234 | 234 | $data = $avatar->data(); |
@@ -238,13 +238,13 @@ discard block |
||
| 238 | 238 | $file = $this->folder->newFile($path); |
| 239 | 239 | $file->putContent($data); |
| 240 | 240 | } catch (NotPermittedException $e) { |
| 241 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 241 | + $this->logger->error('Failed to save avatar for '.$this->user->getUID()); |
|
| 242 | 242 | throw new NotFoundException(); |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - if($this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', null) === null) { |
|
| 247 | + if ($this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', null) === null) { |
|
| 248 | 248 | $generated = $this->folder->fileExists('generated') ? 'true' : 'false'; |
| 249 | 249 | $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', $generated); |
| 250 | 250 | } |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | $white = imagecolorallocate($im, 255, 255, 255); |
| 282 | 282 | imagefilledrectangle($im, 0, 0, $size, $size, $background); |
| 283 | 283 | |
| 284 | - $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.ttf'; |
|
| 284 | + $font = __DIR__.'/../../core/fonts/OpenSans-Semibold.ttf'; |
|
| 285 | 285 | |
| 286 | 286 | $fontSize = $size * 0.4; |
| 287 | 287 | $box = imagettfbbox($fontSize, 0, $font, $text); |
@@ -344,7 +344,7 @@ discard block |
||
| 344 | 344 | $result = array(); |
| 345 | 345 | |
| 346 | 346 | // Splitting evenly the string |
| 347 | - for ($i=0; $i< strlen($hash); $i++) { |
|
| 347 | + for ($i = 0; $i < strlen($hash); $i++) { |
|
| 348 | 348 | // chars in md5 goes up to f, hex:16 |
| 349 | 349 | $result[] = intval(substr($hash, $i, 1), 16) % 16; |
| 350 | 350 | } |
@@ -380,7 +380,7 @@ discard block |
||
| 380 | 380 | |
| 381 | 381 | $finalPalette = array_merge($palette1, $palette2, $palette3); |
| 382 | 382 | |
| 383 | - return $finalPalette[$this->hashToInt($hash, $steps * 3 )]; |
|
| 383 | + return $finalPalette[$this->hashToInt($hash, $steps * 3)]; |
|
| 384 | 384 | } |
| 385 | 385 | |
| 386 | 386 | public function userChanged($feature, $oldValue, $newValue) { |