Added generic avatar using the user's initials
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / avatar / DefaultAvatar.class.php
CommitLineData
320f4a6d
MW
1<?php
2namespace wcf\data\user\avatar;
3use wcf\system\WCF;
d949e64b 4use wcf\util\StringUtil;
320f4a6d
MW
5
6/**
7 * Represents a default avatar.
8 *
9 * @author Marcel Werk
cea1798f 10 * @copyright 2001-2017 WoltLab GmbH
320f4a6d 11 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 12 * @package WoltLabSuite\Core\Data\User\Avatar
320f4a6d
MW
13 */
14class DefaultAvatar implements IUserAvatar {
15 /**
16 * image size
17 * @var integer
18 */
2772d4eb 19 public $size = UserAvatar::AVATAR_SIZE;
320f4a6d 20
d18c3c9b
AE
21 /**
22 * content of the `src` attribute
23 * @var string
24 */
25 protected $src = '';
26
27 /**
28 * DefaultAvatar constructor.
29 *
30 * @param string $username username for use with the 'initials' avatar type
31 */
32 public function __construct($username = '') {
33 if (defined('AVATAR_DEFAULT_TYPE') && AVATAR_DEFAULT_TYPE === 'initials' && !empty($username)) {
34 $words = explode(' ', $username);
35 $count = count($words);
36 if ($count > 1) {
37 // combine the first character of each the first and the last word
38 $text = mb_strtoupper(mb_substr($words[0], 0, 1) . mb_substr($words[$count - 1], 0, 1));
39 }
40 else {
41 // use the first two characters
42 $text = mb_strtoupper(mb_substr($username, 0, 2));
43 }
44
45 $backgroundColor = substr(sha1($username), 0, 6);
46
47 $perceptiveLuminance = $this->getPerceptiveLuminance(
48 hexdec($backgroundColor[0] . $backgroundColor[1]),
49 hexdec($backgroundColor[2] . $backgroundColor[3]),
50 hexdec($backgroundColor[4] . $backgroundColor[5])
51 );
52
53 $textColor = ($perceptiveLuminance < 0.3) ? '000' : 'fff';
54
55 // the <path> is basically a shorter version of a <rect>
56 $svg = <<<SVG
57<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#{$backgroundColor}" d="M0 0h16v16H0z"/><text x="8" y="8" fill="#{$textColor}" text-anchor="middle" alignment-baseline="central" font-family="Arial" font-size="7">{$text}</text></svg>
58SVG;
59
60 $this->src = "data:image/svg+xml;base64," . base64_encode($svg);
61 }
62 else {
63 $this->src = WCF::getPath().'images/avatars/avatar-default.svg';
64 }
65 }
66
320f4a6d 67 /**
0fcfe5f6 68 * @inheritDoc
320f4a6d
MW
69 */
70 public function getURL($size = null) {
d18c3c9b 71 return $this->src;
320f4a6d
MW
72 }
73
74 /**
0fcfe5f6 75 * @inheritDoc
320f4a6d
MW
76 */
77 public function getImageTag($size = null) {
78 if ($size === null) $size = $this->size;
79
e5f9b56c 80 return '<img src="'.StringUtil::encodeHTML($this->getURL($size)).'" style="width: '.$size.'px; height: '.$size.'px" alt="" class="userAvatarImage">';
320f4a6d
MW
81 }
82
83 /**
0fcfe5f6 84 * @inheritDoc
320f4a6d
MW
85 */
86 public function getWidth() {
87 return $this->size;
88 }
89
90 /**
0fcfe5f6 91 * @inheritDoc
320f4a6d
MW
92 */
93 public function getHeight() {
94 return $this->size;
95 }
96
97 /**
0fcfe5f6 98 * @inheritDoc
320f4a6d
MW
99 */
100 public function canCrop() {
101 return false;
102 }
02a067c4
MS
103
104 /**
0fcfe5f6 105 * @inheritDoc
02a067c4
MS
106 */
107 public function getCropImageTag($size = null) {
108 return '';
109 }
d18c3c9b
AE
110
111 /**
112 * Returns the perceived luminance of the given color.
113 *
114 * @param integer $r
115 * @param integer $g
116 * @param integer $b
117 * @return float luminance expressed in a float in the range of 0 and 1
118 */
119 protected function getPerceptiveLuminance($r, $g, $b) {
120 return 1 - (0.299 * $r + 0.587 * $g + 0.114 * $b) / 255;
121 }
320f4a6d 122}