UNPKG

@sync-in/server

Version:

The secure, open-source platform for file storage, sharing, collaboration, and sync

113 lines (112 loc) 3.66 kB
/* * Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com> * This file is part of Sync-in | The open source file sync and share solution * See the LICENSE file for licensing details */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: Object.getOwnPropertyDescriptor(all, name).get }); } _export(exports, { get convertImageToBase64 () { return convertImageToBase64; }, get generateAvatar () { return generateAvatar; }, get generateThumbnail () { return generateThumbnail; }, get pngMimeType () { return pngMimeType; }, get svgMimeType () { return svgMimeType; } }); const _canvas = require("canvas"); const _promises = /*#__PURE__*/ _interop_require_default(require("node:fs/promises")); const _nodepath = /*#__PURE__*/ _interop_require_default(require("node:path")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } (0, _canvas.registerFont)(_nodepath.default.join(__dirname, 'fonts', 'avatar.ttf'), { family: 'Avatar' }); const pngMimeType = 'image/png'; const svgMimeType = 'image/svg+xml'; async function generateThumbnail(filePath, size) { const image = await (0, _canvas.loadImage)(filePath); let width = image.width; let height = image.height; // Calculate the new dimensions, maintaining the aspect ratio if (width > height) { if (width > size) { height *= size / width; width = size; } } else { if (height > size) { width *= size / height; height = size; } } // Set the canvas dimensions to the new dimensions const canvas = (0, _canvas.createCanvas)(width, height); // Draw the resized image on the canvas const ctx = canvas.getContext('2d'); ctx.drawImage(image, 0, 0, width, height); return canvas.createPNGStream({ compressionLevel: 0 }); } function generateAvatar(initials) { const canvas = (0, _canvas.createCanvas)(256, 256); const ctx = canvas.getContext('2d'); const text = initials; const { backgroundColor, foregroundColor } = randomColor(); // Properties ctx.quality = 'best'; // Draw background ctx.fillStyle = backgroundColor; ctx.fillRect(0, 0, canvas.width, canvas.height); // Draw text const height = 150; ctx.font = `${height}px "Avatar"`; ctx.fillStyle = foregroundColor; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(text, canvas.width / 2, canvas.height / 2.1); return canvas.createPNGStream(); } async function convertImageToBase64(imgPath) { const base64String = await _promises.default.readFile(imgPath, { encoding: 'base64' }); return `data:image/png;base64,${base64String}`; } function randomColor() { let color = ''; while(color.length < 6){ /* sometimes the returned value does not have * the 6 digits needed, so we do it again until * it does */ color = Math.floor(Math.random() * 16777215).toString(16); } const red = parseInt(color.substring(0, 2), 16); const green = parseInt(color.substring(2, 4), 16); const blue = parseInt(color.substring(4, 6), 16); const brightness = red * 0.299 + green * 0.587 + blue * 0.114; return { backgroundColor: `#${color}`, foregroundColor: brightness > 180 ? '#000000' : '#ffffff' }; } //# sourceMappingURL=image.js.map