fix: preserve GIF and SVG formats during transforms

This commit is contained in:
MarSeventh
2026-07-29 21:07:54 +08:00
parent cd181cd349
commit d5b3ffb0dd
2 changed files with 16 additions and 4 deletions
+14
View File
@@ -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}`);
}
+2 -4
View File
@@ -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 = {}) {