Commit | Line | Data |
---|---|---|
320f4a6d | 1 | <?php |
a9229942 | 2 | |
320f4a6d | 3 | namespace wcf\data\user\avatar; |
a9229942 | 4 | |
320f4a6d | 5 | use wcf\system\WCF; |
d949e64b | 6 | use wcf\util\StringUtil; |
320f4a6d MW |
7 | |
8 | /** | |
9 | * Represents a default avatar. | |
a9229942 TD |
10 | * |
11 | * @author Marcel Werk | |
12 | * @copyright 2001-2019 WoltLab GmbH | |
13 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
320f4a6d | 14 | */ |
947aeabe | 15 | class DefaultAvatar implements IUserAvatar, ISafeFormatAvatar |
a9229942 TD |
16 | { |
17 | /** | |
18 | * image size | |
19 | * @var int | |
20 | */ | |
21 | public $size = UserAvatar::AVATAR_SIZE; | |
22 | ||
23 | /** | |
24 | * content of the `src` attribute | |
25 | * @var string | |
26 | */ | |
27 | protected $src = ''; | |
28 | ||
29 | /** | |
30 | * DefaultAvatar constructor. | |
31 | * | |
32 | * @param string $username username for use with the 'initials' avatar type | |
33 | */ | |
34 | public function __construct($username = '') | |
35 | { | |
36 | if (\defined('AVATAR_DEFAULT_TYPE') && AVATAR_DEFAULT_TYPE === 'initials' && !empty($username)) { | |
37 | $words = \explode(' ', $username); | |
38 | $count = \count($words); | |
39 | if ($count > 1) { | |
40 | // combine the first character of each the first and the last word | |
41 | $text = \mb_strtoupper(\mb_substr($words[0], 0, 1) . \mb_substr($words[$count - 1], 0, 1)); | |
42 | } else { | |
43 | // use the first two characters | |
44 | $text = \mb_strtoupper(\mb_substr($username, 0, 2)); | |
45 | } | |
46 | ||
47 | $text = \htmlspecialchars($text, \ENT_XML1, 'UTF-8'); | |
48 | ||
49 | $backgroundColor = \substr(\sha1($username), 0, 6); | |
50 | ||
51 | $perceptiveLuminance = $this->getPerceptiveLuminance( | |
52 | \hexdec($backgroundColor[0] . $backgroundColor[1]), | |
53 | \hexdec($backgroundColor[2] . $backgroundColor[3]), | |
54 | \hexdec($backgroundColor[4] . $backgroundColor[5]) | |
55 | ); | |
56 | ||
514d3b19 | 57 | $textColor = ($perceptiveLuminance < 0.5) ? '0 0 0' : '255 255 255'; |
a9229942 TD |
58 | |
59 | // the <path> is basically a shorter version of a <rect> | |
60 | $svg = <<<SVG | |
c422212c | 61 | <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="128" height="128"><path fill="#{$backgroundColor}" d="M0 0h16v16H0z"/><text x="8" y="8" fill="rgba({$textColor} / 0.89)" text-anchor="middle" dy=".3em" font-family="Arial" font-size="7">{$text}</text></svg> |
d18c3c9b | 62 | SVG; |
a9229942 TD |
63 | |
64 | $this->src = "data:image/svg+xml;base64," . \base64_encode($svg); | |
65 | } else { | |
66 | $this->src = WCF::getPath() . 'images/avatars/avatar-default.svg'; | |
67 | } | |
68 | } | |
69 | ||
947aeabe TD |
70 | /** |
71 | * @inheritDoc | |
72 | */ | |
73 | public function getSafeURL(?int $size = null): string | |
74 | { | |
75 | return WCF::getPath() . 'images/avatars/avatar-default.png'; | |
76 | } | |
77 | ||
78 | /** | |
79 | * @inheritDoc | |
80 | */ | |
81 | public function getSafeImageTag(?int $size = null): string | |
82 | { | |
83 | return '<img src="' . StringUtil::encodeHTML($this->getSafeURL($size)) . '" width="' . $size . '" height="' . $size . '" alt="" class="userAvatarImage">'; | |
84 | } | |
85 | ||
a9229942 TD |
86 | /** |
87 | * @inheritDoc | |
88 | */ | |
89 | public function getURL($size = null) | |
90 | { | |
91 | return $this->src; | |
92 | } | |
93 | ||
94 | /** | |
95 | * @inheritDoc | |
96 | */ | |
97 | public function getImageTag($size = null) | |
98 | { | |
99 | if ($size === null) { | |
100 | $size = $this->size; | |
101 | } | |
102 | ||
103 | return '<img src="' . StringUtil::encodeHTML($this->getURL($size)) . '" width="' . $size . '" height="' . $size . '" alt="" class="userAvatarImage">'; | |
104 | } | |
105 | ||
106 | /** | |
107 | * @inheritDoc | |
108 | */ | |
109 | public function getWidth() | |
110 | { | |
111 | return $this->size; | |
112 | } | |
113 | ||
114 | /** | |
115 | * @inheritDoc | |
116 | */ | |
117 | public function getHeight() | |
118 | { | |
119 | return $this->size; | |
120 | } | |
121 | ||
a9229942 TD |
122 | /** |
123 | * Returns the perceived luminance of the given color. | |
124 | * | |
125 | * @param int $r | |
126 | * @param int $g | |
127 | * @param int $b | |
128 | * @return float luminance expressed in a float in the range of 0 and 1 | |
129 | */ | |
130 | protected function getPerceptiveLuminance($r, $g, $b) | |
131 | { | |
132 | return 1 - (0.299 * $r + 0.587 * $g + 0.114 * $b) / 255; | |
133 | } | |
320f4a6d | 134 | } |