mirror of
https://github.com/ZSCGR/CloudFlare-ImgBed.git
synced 2026-08-13 04:03:42 +08:00
fix: preserve GIF and SVG formats during transforms
This commit is contained in:
@@ -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}`);
|
||||
}
|
||||
|
||||
@@ -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 = {}) {
|
||||
|
||||
Reference in New Issue
Block a user