Spaces:
Paused
Paused
File size: 1,763 Bytes
8de10f9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import Jimp from 'jimp';
import fs from 'fs/promises'; // Using promises for async/await compatibility
import path from 'path'; // To handle file paths
const __filename = new URL(import.meta.url).pathname;
const __dirname = path.dirname(__filename);
const generateProfilePictureWithWatermark = async (buffer) => {
try {
const jimp = await Jimp.read(buffer);
const min = jimp.getWidth();
const max = jimp.getHeight();
const cropped = jimp.crop(0, 0, min, max);
// Ensure the watermark path is correct
const watermarkPath = path.resolve(__dirname, 'watermark.png');
// Check if watermark file exists
try {
await fs.access(watermarkPath);
} catch (error) {
throw new Error(`Watermark file not found at ${watermarkPath}`);
}
const watermarkBuffer = await fs.readFile(watermarkPath);
const watermark = await Jimp.read(watermarkBuffer);
// Resize the watermark to a larger size
watermark.scaleToFit(200, 200); // Increase the size here
// Calculate the position to place the watermark (bottom left corner)
const x = 10;
const y = cropped.bitmap.height - watermark.bitmap.height - 10;
// Composite the watermark onto the profile picture
cropped.composite(watermark, x, y);
// Scale the profile picture to fit within 720x720
const scaledImg = await cropped.scaleToFit(720, 720).getBufferAsync(Jimp.MIME_JPEG);
return {
img: scaledImg,
preview: scaledImg, // Assuming the preview is the same as the profile picture
};
} catch (error) {
console.error('Error generating profile picture with watermark:', error);
throw error;
}
};
export default generateProfilePictureWithWatermark;
|