image-utility-library
Version:
A Node.js library for compressing, resizing, cropping, and applying effects to images.
19 lines (16 loc) • 553 B
JavaScript
import sharp from 'sharp';
/**
* Convert an image buffer to a different format.
* @param {Buffer} imageBuffer - The input image buffer.
* @param {String} format - Desired format (jpeg, png, webp).
* @returns {Promise<Buffer>} - Image buffer in the new format.
*/
const formatImage = async (imageBuffer, format) => {
if (!['jpeg', 'png', 'webp'].includes(format)) {
throw new Error('Unsupported format provided.');
}
return await sharp(imageBuffer)
.toFormat(format)
.toBuffer();
};
export default formatImage;