Fix non-PNG images not displaying in Kitty protocol terminals

JPEG/GIF/WebP images were not rendering in terminals using the Kitty
graphics protocol (Kitty, Ghostty, WezTerm) because it requires PNG
format (f=100). Non-PNG images are now converted to PNG using sharp
before being sent to the terminal.
This commit is contained in:
Mario Zechner 2026-01-03 16:58:49 +01:00
parent 9b0ec02405
commit 79c56475e0
3 changed files with 80 additions and 3 deletions

View file

@ -0,0 +1,26 @@
/**
* Convert image to PNG format for terminal display.
* Kitty graphics protocol requires PNG format (f=100).
*/
export async function convertToPng(
base64Data: string,
mimeType: string,
): Promise<{ data: string; mimeType: string } | null> {
// Already PNG, no conversion needed
if (mimeType === "image/png") {
return { data: base64Data, mimeType };
}
try {
const sharp = (await import("sharp")).default;
const buffer = Buffer.from(base64Data, "base64");
const pngBuffer = await sharp(buffer).png().toBuffer();
return {
data: pngBuffer.toString("base64"),
mimeType: "image/png",
};
} catch {
// Sharp not available or conversion failed
return null;
}
}