feat(core): add progressive JPEG support and fix Sharp format switching
This commit is contained in:
@@ -2,6 +2,7 @@ import type * as sharpType from 'sharp';
|
||||
|
||||
export { type sharpType };
|
||||
|
||||
import type { Jimp } from 'jimp';
|
||||
import type * as jimpType from 'jimp';
|
||||
|
||||
export { type jimpType };
|
||||
export { type jimpType, type Jimp };
|
@@ -2,10 +2,11 @@ import * as plugins from './plugins.js';
|
||||
import * as pluginsTyped from './plugins.typed.js';
|
||||
|
||||
export interface IAssetVariation {
|
||||
format?: 'avif' | 'webp' | 'png';
|
||||
format?: 'avif' | 'webp' | 'png' | 'jpeg';
|
||||
width?: number;
|
||||
height?: number;
|
||||
invert?: boolean;
|
||||
progressive?: boolean;
|
||||
}
|
||||
|
||||
export interface ISmartJimpOptions {
|
||||
@@ -50,11 +51,11 @@ export class SmartJimp {
|
||||
}
|
||||
|
||||
jimpMod: typeof pluginsTyped.jimpType;
|
||||
public async getJimpMod(): Promise<typeof pluginsTyped.jimpType.default> {
|
||||
public async getJimpMod(): Promise<typeof pluginsTyped.jimpType> {
|
||||
if (!this.jimpMod) {
|
||||
this.jimpMod = await import('jimp');
|
||||
}
|
||||
return this.jimpMod.default;
|
||||
return this.jimpMod;
|
||||
}
|
||||
|
||||
public async computeAssetVariation(assetBufferArg: Buffer, assetVariationArg: IAssetVariation) {
|
||||
@@ -72,26 +73,53 @@ export class SmartJimp {
|
||||
switch (assetVariationArg.format) {
|
||||
case 'avif':
|
||||
sharpImage = resultResize.avif();
|
||||
break;
|
||||
case 'webp':
|
||||
sharpImage = resultResize.webp();
|
||||
break;
|
||||
case 'png':
|
||||
sharpImage = resultResize.png();
|
||||
break;
|
||||
case 'jpeg':
|
||||
sharpImage = resultResize.jpeg({
|
||||
progressive: assetVariationArg.progressive || false
|
||||
});
|
||||
break;
|
||||
default:
|
||||
// Default to JPEG
|
||||
sharpImage = resultResize.jpeg({
|
||||
progressive: assetVariationArg.progressive || false
|
||||
});
|
||||
}
|
||||
return sharpImage.toBuffer();
|
||||
} else if (this.options.mode === 'jimp') {
|
||||
const jimp = await this.getJimpMod();
|
||||
let jimpImage = await jimp.read(assetBufferArg);
|
||||
const jimpMod = await this.getJimpMod();
|
||||
let jimpImage = await jimpMod.Jimp.read(assetBufferArg);
|
||||
if (assetVariationArg.width || assetVariationArg.height) {
|
||||
jimpImage = jimpImage.resize(assetVariationArg.width, assetVariationArg.height);
|
||||
const resizeOptions: any = {};
|
||||
if (assetVariationArg.width) resizeOptions.w = assetVariationArg.width;
|
||||
if (assetVariationArg.height) resizeOptions.h = assetVariationArg.height;
|
||||
jimpImage.resize(resizeOptions);
|
||||
}
|
||||
if (assetVariationArg.invert) {
|
||||
jimpImage = jimpImage.invert();
|
||||
jimpImage.invert();
|
||||
}
|
||||
// Note: Jimp does not support progressive JPEG encoding
|
||||
// Progressive option is ignored in jimp mode
|
||||
switch (assetVariationArg.format) {
|
||||
case 'png':
|
||||
return await jimpImage.getBufferAsync(jimp.MIME_PNG);
|
||||
return await jimpImage.getBuffer("image/png");
|
||||
case 'jpeg':
|
||||
return await jimpImage.getBuffer("image/jpeg");
|
||||
case 'webp':
|
||||
// Jimp doesn't support WebP, fallback to JPEG
|
||||
return await jimpImage.getBuffer("image/jpeg");
|
||||
case 'avif':
|
||||
// Jimp doesn't support AVIF, fallback to JPEG
|
||||
return await jimpImage.getBuffer("image/jpeg");
|
||||
default:
|
||||
return await jimpImage.getBufferAsync(jimp.MIME_JPEG);
|
||||
// Default to JPEG
|
||||
return await jimpImage.getBuffer("image/jpeg");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user