MCPcopy Create free account
hub / github.com/microsoft/data-formulator / downscaleImageForAgent

Function downscaleImageForAgent

src/app/chartCache.ts:74–96  ·  view source on GitHub ↗
(
    dataUrl: string,
    maxDim: number = 200,
    quality: number = 0.75,
)

Source from the content-addressed store, hash-verified

72 * while preserving the original chart layout.
73 */
74export async function downscaleImageForAgent(
75 dataUrl: string,
76 maxDim: number = 200,
77 quality: number = 0.75,
78): Promise<string> {
79 const img = new Image();
80 await new Promise<void>((resolve, reject) => {
81 img.onload = () => resolve();
82 img.onerror = reject;
83 img.src = dataUrl;
84 });
85 const scale = Math.min(maxDim / img.width, maxDim / img.height, 1);
86 const w = Math.round(img.width * scale);
87 const h = Math.round(img.height * scale);
88 const canvas = document.createElement('canvas');
89 canvas.width = w;
90 canvas.height = h;
91 const ctx = canvas.getContext('2d')!;
92 ctx.fillStyle = 'white';
93 ctx.fillRect(0, 0, w, h);
94 ctx.drawImage(img, 0, 0, w, h);
95 return canvas.toDataURL('image/jpeg', quality);
96}
97
98/**
99 * Compute a deterministic cache key from chart rendering inputs.

Callers 2

getIdeasForVisualizationFunction · 0.90
deriveNewDataFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected