feat(media): add image resizing support for processImage

Extend the `processImage` method to support optional resizing with `width` and `height` parameters. Update processing pipeline to handle resizing while maintaining existing format processing for `webp` and `avif`.
This commit is contained in:
Mathis HERRIOT
2026-01-14 21:44:00 +01:00
parent d7c2a965a0
commit 47d6fcb6a0
3 changed files with 16 additions and 3 deletions

View File

@@ -17,6 +17,7 @@ export interface IMediaService {
processImage(
buffer: Buffer,
format?: "webp" | "avif",
resize?: { width?: number; height?: number },
): Promise<MediaProcessingResult>;
processVideo(
buffer: Buffer,

View File

@@ -83,8 +83,9 @@ export class MediaService implements IMediaService {
async processImage(
buffer: Buffer,
format: "webp" | "avif" = "webp",
resize?: { width?: number; height?: number },
): Promise<MediaProcessingResult> {
return this.imageProcessor.process(buffer, { format });
return this.imageProcessor.process(buffer, { format, resize });
}
async processVideo(

View File

@@ -13,11 +13,22 @@ export class ImageProcessorStrategy implements IMediaProcessorStrategy {
async process(
buffer: Buffer,
options: { format: "webp" | "avif" } = { format: "webp" },
options: {
format: "webp" | "avif";
resize?: { width?: number; height?: number };
} = { format: "webp" },
): Promise<MediaProcessingResult> {
try {
const { format } = options;
const { format, resize } = options;
let pipeline = sharp(buffer);
if (resize) {
pipeline = pipeline.resize(resize.width, resize.height, {
fit: "cover",
position: "center",
});
}
const metadata = await pipeline.metadata();
if (format === "webp") {