From d5b3ffb0dd6f12666b8ce234fedf6d01a425dec1 Mon Sep 17 00:00:00 2001 From: MarSeventh <1193267292@qq.com> Date: Wed, 29 Jul 2026 21:07:54 +0800 Subject: [PATCH] fix: preserve GIF and SVG formats during transforms --- deploy/server/imageProcessor.js | 14 ++++++++++++++ functions/file/imageTransform.js | 6 ++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/deploy/server/imageProcessor.js b/deploy/server/imageProcessor.js index ff78a77c..18f9ed81 100644 --- a/deploy/server/imageProcessor.js +++ b/deploy/server/imageProcessor.js @@ -20,6 +20,18 @@ sharp.concurrency(concurrency); export const dockerImageProcessor = { async transform(stream, options) { const input = await readStreamWithLimit(stream, MAX_INPUT_BYTES); + + // SVG is inherently scalable. Keep the source document unchanged, + // matching Cloudflare Image Transformations behavior. + if (options.sourceType === 'image/svg+xml') { + return new Response(input, { + headers: { + 'Content-Type': options.sourceType, + 'Content-Length': input.byteLength.toString(), + }, + }); + } + const animated = options.sourceType === 'image/gif' || options.sourceType === 'image/webp'; let pipeline = sharp(input, { @@ -62,6 +74,8 @@ function applyOutputFormat(pipeline, outputFormat) { return pipeline.webp(); case 'image/avif': return pipeline.avif(); + case 'image/gif': + return pipeline.gif(); default: throw new Error(`Unsupported Docker image output format: ${outputFormat}`); } diff --git a/functions/file/imageTransform.js b/functions/file/imageTransform.js index 2ddb528c..91b42906 100644 --- a/functions/file/imageTransform.js +++ b/functions/file/imageTransform.js @@ -7,10 +7,8 @@ const OUTPUT_FORMATS = new Map([ ['image/png', 'image/png'], ['image/webp', 'image/webp'], ['image/avif', 'image/avif'], - // Images binding does not encode GIF or SVG. WebP preserves GIF animation - // while PNG preserves SVG transparency. - ['image/gif', 'image/webp'], - ['image/svg+xml', 'image/png'], + ['image/gif', 'image/gif'], + ['image/svg+xml', 'image/svg+xml'], ]); export function parseImageTransform(url, accessConfig = {}) {