@travetto/image
v5.0.17
Published
Image support, resizing, and optimization
Downloads
231
Readme
Image
Image support, resizing, and optimization
Install: @travetto/image
npm install @travetto/image
# or
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).
Sharp
The in process operations leverage sharp and will perform within expectations, and will execute substantially faster than invoking a subprocess.
Code: Simple Image Resize
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<string> {
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;
}
}