aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Avatar/Avatar.php
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2022-08-30 13:27:21 +0200
committerCarl Schwan <carl@carlschwan.eu>2022-09-09 13:37:51 +0200
commitf98ae2b5b0567f28a875106724c0475d6395f3c5 (patch)
treebc21874832e15741f4c5a9545138b3044f7c340b /lib/private/Avatar/Avatar.php
parent76f42e121b7aee17508feeb8e86ded55b9b78736 (diff)
downloadnextcloud-server-f98ae2b5b0567f28a875106724c0475d6395f3c5.tar.gz
nextcloud-server-f98ae2b5b0567f28a875106724c0475d6395f3c5.zip
Avatar new style
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Diffstat (limited to 'lib/private/Avatar/Avatar.php')
-rw-r--r--lib/private/Avatar/Avatar.php37
1 files changed, 22 insertions, 15 deletions
diff --git a/lib/private/Avatar/Avatar.php b/lib/private/Avatar/Avatar.php
index 0eb8f8816d8..aec0f6e10f4 100644
--- a/lib/private/Avatar/Avatar.php
+++ b/lib/private/Avatar/Avatar.php
@@ -59,7 +59,7 @@ abstract class Avatar implements IAvatar {
private string $svgTemplate = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="{size}" height="{size}" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="#{fill}"></rect>
- <text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#fff">{letter}</text>
+ <text x="50%" y="350" style="font-weight:normal;font-size:280px;font-family:\'Noto Sans\';text-anchor:middle;fill:#{fgFill}">{letter}</text>
</svg>';
public function __construct(LoggerInterface $logger) {
@@ -88,9 +88,9 @@ abstract class Avatar implements IAvatar {
/**
* @inheritdoc
*/
- public function get(int $size = 64) {
+ public function get(int $size = 64, bool $darkTheme = false) {
try {
- $file = $this->getFile($size);
+ $file = $this->getFile($size, $darkTheme);
} catch (NotFoundException $e) {
return false;
}
@@ -111,25 +111,27 @@ abstract class Avatar implements IAvatar {
* @return string
*
*/
- protected function getAvatarVector(int $size): string {
+ protected function getAvatarVector(int $size, bool $dark): string {
$userDisplayName = $this->getDisplayName();
- $bgRGB = $this->avatarBackgroundColor($userDisplayName);
- $bgHEX = sprintf("%02x%02x%02x", $bgRGB->red(), $bgRGB->green(), $bgRGB->blue());
+ $fgRGB = $this->avatarBackgroundColor($userDisplayName);
+ $bgRGB = $fgRGB->alphaBlending(0.1, $dark ? new Color(0, 0, 0) : new Color(255, 255, 255));
+ $fill = sprintf("%02x%02x%02x", $bgRGB->red(), $bgRGB->green(), $bgRGB->blue());
+ $fgFill = sprintf("%02x%02x%02x", $fgRGB->red(), $fgRGB->green(), $fgRGB->blue());
$text = $this->getAvatarText();
- $toReplace = ['{size}', '{fill}', '{letter}'];
- return str_replace($toReplace, [$size, $bgHEX, $text], $this->svgTemplate);
+ $toReplace = ['{size}', '{fill}', '{fgFill}', '{letter}'];
+ return str_replace($toReplace, [$size, $fill, $fgFill, $text], $this->svgTemplate);
}
/**
* Generate png avatar from svg with Imagick
*/
- protected function generateAvatarFromSvg(int $size): ?string {
+ protected function generateAvatarFromSvg(int $size, bool $dark): ?string {
if (!extension_loaded('imagick')) {
return null;
}
try {
- $font = __DIR__ . '/../../core/fonts/NotoSans-Regular.ttf';
- $svg = $this->getAvatarVector($size);
+ $font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
+ $svg = $this->getAvatarVector($size, $dark);
$avatar = new Imagick();
$avatar->setFont($font);
$avatar->readImageBlob($svg);
@@ -145,9 +147,10 @@ abstract class Avatar implements IAvatar {
/**
* Generate png avatar with GD
*/
- protected function generateAvatar(string $userDisplayName, int $size): string {
+ protected function generateAvatar(string $userDisplayName, int $size, bool $dark): string {
$text = $this->getAvatarText();
- $backgroundColor = $this->avatarBackgroundColor($userDisplayName);
+ $textColor = $this->avatarBackgroundColor($userDisplayName);
+ $backgroundColor = $textColor->alphaBlending(0.1, $dark ? new Color() : new Color(255, 255, 255));
$im = imagecreatetruecolor($size, $size);
$background = imagecolorallocate(
@@ -156,7 +159,11 @@ abstract class Avatar implements IAvatar {
$backgroundColor->green(),
$backgroundColor->blue()
);
- $white = imagecolorallocate($im, 255, 255, 255);
+ $textColor = imagecolorallocate($im,
+ $textColor->red(),
+ $textColor->green(),
+ $textColor->blue()
+ );
imagefilledrectangle($im, 0, 0, $size, $size, $background);
$font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
@@ -166,7 +173,7 @@ abstract class Avatar implements IAvatar {
$im, $text, $font, (int)$fontSize
);
- imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text);
+ imagettftext($im, $fontSize, 0, $x, $y, $textColor, $font, $text);
ob_start();
imagepng($im);