image-utility-library
Version:
A Node.js library for compressing, resizing, cropping, and applying effects to images.
70 lines (62 loc) • 2.63 kB
JavaScript
// // Import Jimp namespace
// import * as Jimp from 'jimp';
// /**
// * Apply advanced effects to an image.
// * @param {Buffer} imageBuffer - Input image buffer.
// * @param {String} effect - Effect to apply (grayscale, invert, sepia).
// * @returns {Promise<Buffer>} - Image buffer with the applied effect.
// */
// const applyEffects = async (imageBuffer, effect) => {
// try {
// // Validate inputs
// if (!imageBuffer || !(imageBuffer instanceof Buffer)) {
// throw new Error('Invalid image buffer provided.');
// }
// if (!effect || typeof effect !== 'string') {
// throw new Error('Invalid effect type provided.');
// }
// // Read the image using Jimp
// // When importing as a namespace, we access the read method this way
// const image = await Jimp.default.read(imageBuffer);
// // Apply the requested effect
// switch (effect.toLowerCase()) {
// case 'grayscale':
// image.greyscale();
// break;
// case 'invert':
// image.invert();
// break;
// case 'sepia':
// if (typeof image.sepia === 'function') {
// image.sepia();
// } else {
// // Manual sepia implementation as fallback
// image.scan(0, 0, image.bitmap.width, image.bitmap.height, function(x, y, idx) {
// const red = this.bitmap.data[idx + 0];
// const green = this.bitmap.data[idx + 1];
// const blue = this.bitmap.data[idx + 2];
// const newRed = Math.min(255, (red * 0.393) + (green * 0.769) + (blue * 0.189));
// const newGreen = Math.min(255, (red * 0.349) + (green * 0.686) + (blue * 0.168));
// const newBlue = Math.min(255, (red * 0.272) + (green * 0.534) + (blue * 0.131));
// this.bitmap.data[idx + 0] = newRed;
// this.bitmap.data[idx + 1] = newGreen;
// this.bitmap.data[idx + 2] = newBlue;
// });
// }
// break;
// default:
// throw new Error(`Unsupported effect provided: ${effect}. Supported effects are: grayscale, invert, sepia.`);
// }
// // Use the constants correctly
// return await new Promise((resolve, reject) => {
// image.getBuffer(Jimp.default.MIME_PNG, (err, buffer) => {
// if (err) reject(err);
// else resolve(buffer);
// });
// });
// } catch (error) {
// console.error('Error applying effect:', error);
// throw error;
// }
// };
// export default applyEffects;