char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Linting & Formatting scripts (#26819)92d1bb0c9

char/aliceai-t5-moe
1.9 KiB74 linesraw
1import { MimeTypeImage } from '$lib/enums';
2
3/**
4 * Convert an SVG base64 data URL to a PNG data URL
5 * @param base64UrlSvg - The SVG base64 data URL to convert
6 * @param backgroundColor - Background color for the PNG (default: 'white')
7 * @returns Promise resolving to PNG data URL
8 */
9export function svgBase64UrlToPngDataURL(
10	base64UrlSvg: string,
11	backgroundColor: string = 'white'
12): Promise<string> {
13	return new Promise((resolve, reject) => {
14		try {
15			const img = new Image();
16
17			img.onload = () => {
18				const canvas = document.createElement('canvas');
19				const ctx = canvas.getContext('2d');
20
21				if (!ctx) {
22					reject(new Error('Failed to get 2D canvas context.'));
23
24					return;
25				}
26
27				const targetWidth = img.naturalWidth || 300;
28				const targetHeight = img.naturalHeight || 300;
29
30				canvas.width = targetWidth;
31				canvas.height = targetHeight;
32
33				if (backgroundColor) {
34					ctx.fillStyle = backgroundColor;
35					ctx.fillRect(0, 0, canvas.width, canvas.height);
36				}
37
38				ctx.drawImage(img, 0, 0, targetWidth, targetHeight);
39
40				resolve(canvas.toDataURL(MimeTypeImage.PNG));
41			};
42
43			img.onerror = () => {
44				reject(new Error('Failed to load SVG image. Ensure the SVG data is valid.'));
45			};
46
47			img.src = base64UrlSvg;
48		} catch (error) {
49			const message = error instanceof Error ? error.message : String(error);
50			const errorMessage = `Error converting SVG to PNG: ${message}`;
51
52			console.error(errorMessage, error);
53			reject(new Error(errorMessage));
54		}
55	});
56}
57
58/**
59 * Check if a file is an SVG based on its MIME type
60 * @param file - The file to check
61 * @returns True if the file is an SVG
62 */
63export function isSvgFile(file: File): boolean {
64	return file.type === MimeTypeImage.SVG;
65}
66
67/**
68 * Check if a MIME type represents an SVG
69 * @param mimeType - The MIME type to check
70 * @returns True if the MIME type is image/svg+xml
71 */
72export function isSvgMimeType(mimeType: string): boolean {
73	return mimeType === MimeTypeImage.SVG;
74}