Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate avatars for display name with chinese characters #42534

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added core/fonts/NotoSansSC-Bold.ttf
Binary file not shown.
Binary file added core/fonts/NotoSansSC-Regular.ttf
Binary file not shown.
32 changes: 16 additions & 16 deletions lib/private/Avatar/Avatar.php
Expand Up @@ -100,19 +100,7 @@ public function get(int $size = 64, bool $darkTheme = false) {
return $avatar;
}

/**
* {size} = 500
* {fill} = hex color to fill
* {letter} = Letter to display
*
* Generate SVG avatar
*
* @param int $size The requested image size in pixel
* @return string
*
*/
protected function getAvatarVector(int $size, bool $darkTheme): string {
$userDisplayName = $this->getDisplayName();
protected function getAvatarVector(string $userDisplayName, int $size, bool $darkTheme): string {
$fgRGB = $this->avatarBackgroundColor($userDisplayName);
$bgRGB = $fgRGB->alphaBlending(0.1, $darkTheme ? new Color(0, 0, 0) : new Color(255, 255, 255));
$fill = sprintf("%02x%02x%02x", $bgRGB->red(), $bgRGB->green(), $bgRGB->blue());
Expand All @@ -122,6 +110,14 @@ protected function getAvatarVector(int $size, bool $darkTheme): string {
return str_replace($toReplace, [$size, $fill, $fgFill, $text], $this->svgTemplate);
}

protected function getFont(string $userDisplayName) {
if (preg_match('/\p{Han}/u', $userDisplayName) === 1) {
return __DIR__ . '/../../../core/fonts/NotoSansSC-Regular.ttf';
}

return __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
}

/**
* Generate png avatar from svg with Imagick
*/
Expand All @@ -134,9 +130,12 @@ protected function generateAvatarFromSvg(int $size, bool $darkTheme): ?string {
if (in_array("RSVG", $formats, true)) {
return null;
}

$userDisplayName = $this->getDisplayName();
$font = $this->getFont($userDisplayName);

try {
$font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
$svg = $this->getAvatarVector($size, $darkTheme);
$svg = $this->getAvatarVector($userDisplayName, $size, $darkTheme);
$avatar = new Imagick();
$avatar->setFont($font);
$avatar->readImageBlob($svg);
Expand Down Expand Up @@ -171,7 +170,8 @@ protected function generateAvatar(string $userDisplayName, int $size, bool $dark
);
imagefilledrectangle($im, 0, 0, $size, $size, $background);

$font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';

$font = $this->getFont($userDisplayName);

$fontSize = $size * 0.4;
[$x, $y] = $this->imageTTFCenter(
Expand Down