@travetto/image
v4.0.6
Published
Image support, resizing, and optimization
Downloads
207
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 png optimization. This is primarily meant to be used in conjunction with other modules, like the Asset module or 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).
The utility's primary structure revolves around the CommandOperation from the Command module. The CommandOperation allows for declaration of a local executable, and a fall-back docker container (mainly meant for development). The ImageConverter utilizes ImageMagick, pngquant, and Jpegoptim as the backing for image resizing and png compression, respectively.
Code: Simple Image Resize
import { createReadStream } from 'node:fs';
import { StreamUtil } from '@travetto/base';
import { ImageConverter } from '@travetto/image';
export class ResizeService {
async resizeImage(imgPath: string, width: number, height: number): Promise<string> {
const stream = await ImageConverter.resize(createReadStream(imgPath), { w: width, h: height });
const out = imgPath.replace(/[.][^.]+$/, (ext) => `.resized${ext}`);
await StreamUtil.writeToFile(stream, out);
return out;
}
}