Image support, resizing, and optimization
npm install @travetto/imageInstall: @travetto/image
``bash
npm install @travetto/image
yarn add @travetto/image
`
This module provides functionality for image resizing, and image optimization. This is primarily meant to be used in conjunction with other modules, like the Email Compilation Support module. It can also be invoked directly as needed (as it can be very handy for batch processing images on the command line).
Code: Simple Image Resize
`typescript
import { createReadStream, createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
import { ImageUtil } from '@travetto/image';
export class ResizeService {
async resizeImage(imgPath: string, width: number, height: number): Promise
const stream = await ImageUtil.convert(createReadStream(imgPath), { w: width, h: height });
const out = imgPath.replace(/[.][^.]+$/, (ext) => .resized${ext});``
await pipeline(stream, createWriteStream(out));
return out;
}
}