MCPcopy Create free account
hub / github.com/TeleBoxOrg/TeleBox_Plugins / fillRoundedCorners

Function fillRoundedCorners

speedlink/speedlink.ts:19–74  ·  view source on GitHub ↗
(
  inputPath: string,
  outPath?: string,
  bgColor: string = "#212338",
  borderPx: number = 14
)

Source from the content-addressed store, hash-verified

17} from "@utils/pathHelpers";
18
19// --- Global variables for test control ---
20let DEFAULT_TIMEOUT = 300000; // Default 5 minutes, can be customized
21
22async function fillRoundedCorners(
23 inputPath: string,
24 outPath?: string,
25 bgColor: string = "#212338",
26 borderPx: number = 14
27) {
28 const meta = await sharp(inputPath).metadata();
29
30 const output =
31 outPath ??
32 (() => {
33 const dir = path.dirname(inputPath);
34 const ext =
35 (meta.format as string) === "jpeg" || (meta.format as string) === "jpg" ? ".jpg" : ".png";
36 const base = path.basename(inputPath, path.extname(inputPath));
37 return path.join(dir, `${base}.filled${ext}`);
38 })();
39
40 const width = meta.width ?? 0;
41 const height = meta.height ?? 0;
42 if (!width || !height) {
43 throw new Error("Unable to read image dimensions");
44 }
45
46 const maxInset = Math.floor((Math.min(width, height) - 1) / 2);
47 const inset = Math.max(0, Math.min(borderPx, maxInset));
48 const cropW = width - inset * 2;
49 const cropH = height - inset * 2;
50
51 const background = sharp({
52 create: {
53 width,
54 height,
55 channels: 4,
56 background: bgColor,
57 },
58 });
59
60 const innerBuf = await sharp(inputPath)
61 .extract({ left: inset, top: inset, width: cropW, height: cropH })
62 .toBuffer();
63
64 const left = Math.floor((width - cropW) / 2);
65 const top = Math.floor((height - cropH) / 2);
66
67 let composed = background.composite([{ input: innerBuf, left, top }]);
68
69 if ((meta.format as string) === "jpeg" || (meta.format as string) === "jpg") {
70 composed = composed.jpeg({ quality: 95 });
71 } else if (meta.format === "png" || !meta.format) {
72 composed = composed.png({ compressionLevel: 9 });
73 }
74
75 await composed.toFile(output);
76 return { output };

Callers 1

saveSpeedtestImageFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected