Skip to content

Commit 40b5bae

Browse files
committed
fix(chat): send original image data in vision requests
Avoid lossy canvas resize/re-encode in the web chat uploader so vision models receive original image bytes and can reliably parse image inputs. Made-with: Cursor
1 parent 4b50158 commit 40b5bae

File tree

1 file changed

+2
-35
lines changed

1 file changed

+2
-35
lines changed

web/src/pages/Chat.tsx

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,12 @@ const isVisionModel = computed(() => {
3232
return m?.pipeline_tag === "image-text-to-text" && m?.has_mmproj === true;
3333
});
3434

35-
const MAX_IMAGE_DIM = 1024;
36-
const THUMB_DIM = 480;
37-
3835
function normalizeImage(file: File): Promise<{ full: string; thumb: string }> {
3936
return new Promise((resolve, reject) => {
4037
const reader = new FileReader();
4138
reader.onload = () => {
42-
const img = new (window.Image as typeof HTMLImageElement)();
43-
img.onload = () => {
44-
try {
45-
let { width, height } = img;
46-
if (!width || !height) { width = width || 512; height = height || 512; }
47-
if (width > MAX_IMAGE_DIM || height > MAX_IMAGE_DIM) {
48-
const scale = MAX_IMAGE_DIM / Math.max(width, height);
49-
width = Math.round(width * scale);
50-
height = Math.round(height * scale);
51-
}
52-
const canvas = document.createElement("canvas");
53-
canvas.width = width;
54-
canvas.height = height;
55-
const ctx = canvas.getContext("2d")!;
56-
ctx.drawImage(img, 0, 0, width, height);
57-
const full = canvas.toDataURL("image/jpeg", 0.85);
58-
59-
const ts = Math.min(THUMB_DIM, width, height);
60-
const tw = Math.round(width * (ts / Math.max(width, height)));
61-
const th = Math.round(height * (ts / Math.max(width, height)));
62-
canvas.width = tw;
63-
canvas.height = th;
64-
ctx.drawImage(img, 0, 0, tw, th);
65-
const thumb = canvas.toDataURL("image/jpeg", 0.6);
66-
67-
resolve({ full, thumb });
68-
} catch (e) {
69-
reject(new Error("Canvas conversion failed: " + (e as Error).message));
70-
}
71-
};
72-
img.onerror = () => reject(new Error("Failed to load image"));
73-
img.src = reader.result as string;
39+
const dataURL = reader.result as string;
40+
resolve({ full: dataURL, thumb: dataURL });
7441
};
7542
reader.onerror = () => reject(new Error("Failed to read file"));
7643
reader.readAsDataURL(file);

0 commit comments

Comments
 (0)