diff --git a/src/client/Utils.ts b/src/client/Utils.ts index a842a42af..7352c388f 100644 --- a/src/client/Utils.ts +++ b/src/client/Utils.ts @@ -296,5 +296,23 @@ export async function getSvgAspectRatio(src: string): Promise { // fetch may fail due to CORS or non-SVG.. } + const imgRatio = await new Promise((resolve) => { + const img = new Image(); + img.onload = () => { + if (img.naturalWidth > 0 && img.naturalHeight > 0) { + resolve(img.naturalWidth / img.naturalHeight); + } else { + resolve(null); + } + }; + img.onerror = () => resolve(null); + img.src = src; + }); + + if (imgRatio !== null) { + self.svgAspectRatioCache.set(src, imgRatio); + return imgRatio; + } + return null; }