Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:users): unify avatar rendering to 512px and tune dynamic font scaling

johaven6ecd91d4cf54
diff --git a/backend/src/common/image.ts b/backend/src/common/image.ts
index 15af8b65..54515a17 100644
--- a/backend/src/common/image.ts
+++ b/backend/src/common/image.ts
@@ -16,7 +16,7 @@ export const imgMimeTypePrefix = 'image/'
export const pngMimeType = 'image/png'
export const svgMimeType = 'image/svg+xml'
export const webpMimeType = 'image/webp'
-const avatarSize = 256
+const avatarSize = 512
const fontPath = path.join(__dirname, 'fonts', 'avatar.ttf')
const loadTextToSVG = promisify(TextToSVG.load.bind(TextToSVG))
let textToSvgCache: Promise<TextToSVG> | null = null
@@ -42,7 +42,7 @@ export async function generateThumbnail(filePath: string, size: number): Promise
export async function generateAvatar(initials: string): Promise<NodeJS.ReadableStream> {
const tts = await getTextToSvg()
const { backgroundColor, foregroundColor } = randomColor()
- const fontSize = fitFontSize(tts, initials, avatarSize * 0.8, 170)
+ const fontSize = fitFontSize(tts, initials, avatarSize * 0.67)
const d = tts.getD(initials, {
x: avatarSize / 2,
@@ -66,7 +66,8 @@ export async function convertImageToBase64(imgPath: string) {
return `data:image/png;base64,${base64String}`
}
-export async function convertTempImageToPng(temporaryImagePath: string, outputPngPath: string, size = 256): Promise<void> {
+export async function convertTempImageToPng(temporaryImagePath: string, outputPngPath: string, size?: number): Promise<void> {
+ size ??= avatarSize
const srcBuffer = await fs.readFile(temporaryImagePath)
const pngBuffer = await sharp(srcBuffer).rotate().resize(size, size, { fit: 'cover' }).png().toBuffer()
await fs.writeFile(temporaryImagePath, pngBuffer)
@@ -93,9 +94,9 @@ function randomColor() {
}
}
-function fitFontSize(tts: TextToSVG, text: string, box: number, start = 170): number {
- // Heuristic to make the text occupy ~80% of the available width
- let size = start
+function fitFontSize(tts: TextToSVG, text: string, box: number, start = box): number {
+ // Heuristic for fitting text to the available width
+ let size = Math.max(20, Math.floor(start))
// Lower bound to prevent infinite loops when the font renders very small
while (size > 20) {
const m = tts.getMetrics(text, { fontSize: size, anchor: 'center middle' })