Spaces:
Runtime error
Runtime error
File size: 1,926 Bytes
6946674 a7fe81f a6e7e8f 6a839c1 a6e7e8f 61988b7 923f391 142f91b 61988b7 142f91b 40e1c88 6946674 142f91b 40e1c88 142f91b 40e1c88 142f91b 923f391 142f91b a7fe81f ad4628d a7fe81f be26971 3205dff 142f91b be26971 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import { PUBLIC_API_BASE } from '$env/static/public';
import { GRID_SIZE, FRAME_SIZE } from '$lib/constants';
export function base64ToBlob(base64image: string): Promise<Blob> {
return new Promise((resolve) => {
const img = new Image();
img.onload = async () => {
const w = img.width;
const h = img.height;
const canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
ctx.drawImage(img, 0, 0, w, h);
const imgBlob: Blob = await new Promise((r) =>
canvas.toBlob(r as BlobCallback, 'image/jpeg', 0.95)
);
resolve(imgBlob);
};
img.src = base64image;
});
}
export async function uploadImage(imagBlob: Blob, params: {
prompt: string;
position: { x: number; y: number };
date: number;
id: string;
room: string;
}): Promise<{
url: string;
filename: string;
}> {
// simple regex slugify string for file name
const promptSlug = slugify(params.prompt);
const key = `${params.position.x}_${params.position.y}`;
const fileName = `sd-${params.id}-${promptSlug}-${key}.jpeg`;
const file = new File([imagBlob], fileName, { type: 'image/jpeg' });
const formData = new FormData()
formData.append('file', file)
const response = await fetch(PUBLIC_API_BASE + "/uploadfile", {
method: 'POST',
body: formData
});
const res = await response.json();
return res;
}
export function round(pos: number, canvasSize: {
width: number;
height: number;
}) {
const max = canvasSize.width - FRAME_SIZE
const value = pos % GRID_SIZE < GRID_SIZE / 2 ? pos - (pos % GRID_SIZE) : pos + GRID_SIZE - (pos % GRID_SIZE);
return Math.max(0, Math.min(Math.round(value), max))
}
function slugify(text: string) {
if (!text) return '';
return text
.toString()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '');
} |