@ng-easy/image-optimizer
Node library that implements image optimizations for the web. It uses internally the super-fast sharp library.
Usage
import fs from 'fs-extra';
import { getImageOptimizer, ImageOptimizerOptions, ImageOptimizer, ImageCache, FilesystemImageCache } from '@ng-easy/image-optimizer';
// Read the original image from the filesystem
const imageAssetPath = 'image.jpg';
const originalImage: Buffer = await fs.readFile(imageAssetPath);
// Get the optimizer and instantiate a cache (optional)
const imageOptimizer: ImageOptimizer = getImageOptimizer(imageAssetPath, originalImage);
const imageCache: ImageCache = new FilesystemImageCache(path.join(process.cwd(), 'tmp'));
// Run the optimization and get the resulting buffer
const options: ImageOptimizerOptions = { format: 'webp', width: 1080, quality: 70 };
const optimizedImage = await imageOptimizer.optimize(originalImage, imageOptimizationTest.options, imageCache);
Supported file types
- jpg, webp, heif, avif
- Pending: png