12f4603a822c6adb98c8dd17ef66114e26a63b72
3 namespace wcf\data\user\avatar
;
6 use wcf\util\StringUtil
;
9 * Represents a default avatar.
12 * @copyright 2001-2019 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 class DefaultAvatar
implements IUserAvatar
, ISafeFormatAvatar
21 public $size = UserAvatar
::AVATAR_SIZE
;
24 * content of the `src` attribute
30 * DefaultAvatar constructor.
32 * @param string $username username for use with the 'initials' avatar type
34 public function __construct($username = '')
36 if (\
defined('AVATAR_DEFAULT_TYPE') && AVATAR_DEFAULT_TYPE
=== 'initials' && !empty($username)) {
37 $words = \
explode(' ', $username);
38 $count = \
count($words);
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));
43 // use the first two characters
44 $text = \
mb_strtoupper(\
mb_substr($username, 0, 2));
47 $text = \
htmlspecialchars($text, \ENT_XML1
, 'UTF-8');
49 $backgroundColor = \
substr(\
sha1($username), 0, 6);
51 $perceptiveLuminance = $this->getPerceptiveLuminance(
52 \
hexdec($backgroundColor[0] . $backgroundColor[1]),
53 \
hexdec($backgroundColor[2] . $backgroundColor[3]),
54 \
hexdec($backgroundColor[4] . $backgroundColor[5])
57 $textColor = ($perceptiveLuminance < 0.5) ?
'0 0 0' : '255 255 255';
59 // the <path> is basically a shorter version of a <rect>
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>
64 $this->src
= "data:image/svg+xml;base64," . \base
64_encode
($svg);
66 $this->src
= WCF
::getPath() . 'images/avatars/avatar-default.svg';
73 public function getSafeURL(?
int $size = null): string
75 return WCF
::getPath() . 'images/avatars/avatar-default.png';
81 public function getSafeImageTag(?
int $size = null): string
83 return '<img src="' . StringUtil
::encodeHTML($this->getSafeURL($size)) . '" width="' . $size . '" height="' . $size . '" alt="" class="userAvatarImage">';
89 public function getURL($size = null)
97 public function getImageTag($size = null)
103 return '<img src="' . StringUtil
::encodeHTML($this->getURL($size)) . '" width="' . $size . '" height="' . $size . '" alt="" class="userAvatarImage">';
109 public function getWidth()
117 public function getHeight()
123 * Returns the perceived luminance of the given color.
128 * @return float luminance expressed in a float in the range of 0 and 1
130 protected function getPerceptiveLuminance($r, $g, $b)
132 return 1 - (0.299 * $r +
0.587 * $g +
0.114 * $b) / 255;