From 6a60f506726d89193347fd7b90a984c6ccae3a22 Mon Sep 17 00:00:00 2001 From: Mailo Date: Mon, 24 Nov 2025 15:36:26 +0100 Subject: [PATCH] Clamp ICO image size to 256x256 to prevent exceeding format limits. --- src/lib/workers/magick.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/lib/workers/magick.ts b/src/lib/workers/magick.ts index a2221ea..9d77b79 100644 --- a/src/lib/workers/magick.ts +++ b/src/lib/workers/magick.ts @@ -293,17 +293,31 @@ const magickConvert = async ( let fmt = to.slice(1).toUpperCase(); if (fmt === "JFIF") fmt = "JPEG"; + // ✅ ICO size clamp to avoid WidthOrHeightExceedsLimit + if (fmt === "ICO") { + const max = 256; + const w = img.width; + const h = img.height; + + if (w > max || h > max) { + const scale = max / Math.max(w, h); + const newW = Math.max(1, Math.round(w * scale)); + const newH = Math.max(1, Math.round(h * scale)); + + img.resize(newW, newH); + } + } + const result = await new Promise((resolve, reject) => { try { - // magick-wasm automatically clamps (https://github.com/dlemstra/magick-wasm/blob/76fc6f2b0c0497d2ddc251bbf6174b4dc92ac3ea/src/magick-image.ts#L2480) if (compression) img.quality = compression; if (!keepMetadata) img.strip(); img.write(fmt as unknown as MagickFormat, (o: Uint8Array) => { resolve(structuredClone(o)); }); - } catch (error) { - reject(error); + } catch (e) { + reject(e); } });