From de2cd55b338d48de055f134e304dfbe10c3ca29e Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Fri, 27 Nov 2026 10:30:00 +0000 Subject: [PATCH] fix(frontend): open non-raster images in a new tab as PNG (#12697) The image viewer's "Open in new tab" turns an inline image into a blob URL. Only plain raster types are opened as they are; anything else, SVG included, is re-encoded to PNG through the canvas the copy action already uses. Co-authored-by: Claude Opus 5.5 --- .../common/image/ImageViewer.svelte | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/frontend/src/lib/components/common/image/ImageViewer.svelte b/frontend/src/lib/components/common/image/ImageViewer.svelte index f7b564c22d..c50254a073 100644 --- a/frontend/src/lib/components/common/image/ImageViewer.svelte +++ b/frontend/src/lib/components/common/image/ImageViewer.svelte @@ -246,8 +246,11 @@ return } // Browsers refuse to open a data: URL as a page; a blob URL of the same bytes opens. + // The blob URL takes this page's origin, so only plain raster types open as they are; + // anything else, SVG included since it opens as a document, is re-encoded to PNG. try { - const url = URL.createObjectURL(await fetchBlob()) + const blob = await fetchBlob() + const url = URL.createObjectURL(RASTER_TYPES.has(blob.type) ? blob : await rasterize()) window.open(url, '_blank', 'noopener') setTimeout(() => URL.revokeObjectURL(url), 60_000) } catch (e) { @@ -255,16 +258,30 @@ } } + const RASTER_TYPES = new Set([ + 'image/png', + 'image/jpeg', + 'image/gif', + 'image/webp', + 'image/avif', + 'image/bmp' + ]) + + /** The displayed image as PNG bytes, drawn through a canvas. */ + function rasterize(): Promise { + const canvas = document.createElement('canvas') + canvas.width = natural?.w ?? imgEl!.naturalWidth + canvas.height = natural?.h ?? imgEl!.naturalHeight + canvas.getContext('2d')!.drawImage(imgEl!, 0, 0, canvas.width, canvas.height) + return new Promise((resolve, reject) => + canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('encoding failed'))), 'image/png') + ) + } + async function copyImage() { try { // The clipboard takes PNG in every browser, other types in few: re-encode through a canvas. - const canvas = document.createElement('canvas') - canvas.width = imgEl!.naturalWidth - canvas.height = imgEl!.naturalHeight - canvas.getContext('2d')!.drawImage(imgEl!, 0, 0) - const blob = new Promise((resolve, reject) => - canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('encoding failed'))), 'image/png') - ) + const blob = rasterize() // Handed over as a promise so `write` runs within the click: Safari refuses it once // the user gesture has expired, which an awaited encode would let happen. await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })])