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 = {
|
export const dockerImageProcessor = {
|
||||||
async transform(stream, options) {
|
async transform(stream, options) {
|
||||||
const input = await readStreamWithLimit(stream, MAX_INPUT_BYTES);
|
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';
|
const animated = options.sourceType === 'image/gif' || options.sourceType === 'image/webp';
|
||||||
|
|
||||||
let pipeline = sharp(input, {
|
let pipeline = sharp(input, {
|
||||||
@@ -62,6 +74,8 @@ function applyOutputFormat(pipeline, outputFormat) {
|
|||||||
return pipeline.webp();
|
return pipeline.webp();
|
||||||
case 'image/avif':
|
case 'image/avif':
|
||||||
return pipeline.avif();
|
return pipeline.avif();
|
||||||
|
case 'image/gif':
|
||||||
|
return pipeline.gif();
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unsupported Docker image output format: ${outputFormat}`);
|
throw new Error(`Unsupported Docker image output format: ${outputFormat}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,8 @@ const OUTPUT_FORMATS = new Map([
|
|||||||
['image/png', 'image/png'],
|
['image/png', 'image/png'],
|
||||||
['image/webp', 'image/webp'],
|
['image/webp', 'image/webp'],
|
||||||
['image/avif', 'image/avif'],
|
['image/avif', 'image/avif'],
|
||||||
// Images binding does not encode GIF or SVG. WebP preserves GIF animation
|
['image/gif', 'image/gif'],
|
||||||
// while PNG preserves SVG transparency.
|
['image/svg+xml', 'image/svg+xml'],
|
||||||
['image/gif', 'image/webp'],
|
|
||||||
['image/svg+xml', 'image/png'],
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
export function parseImageTransform(url, accessConfig = {}) {
|
export function parseImageTransform(url, accessConfig = {}) {
|
||||||
|
|||||||
Reference in New Issue
Block a user