简体   繁体   中英

How to print different text on more than one image using jimp in node js?

I want to create a gift card to send many email with different text, I am using the below logic its work to print but overwrite with previous one image gengerated.

            Jimp.read(fileName)
            .then(function (image) {
                loadedImage1 = image;
                return Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
            })
            .then(font => {

                console.log('[][][', emailData);
                var paths = [];
                for(let i=0; i< Object.keys(emailData['recipient']).length; i++){
                    var receipentName = "Hello "+emailData['recipient'][i].name+', ';
                    loadedImages[i]= Object.assign(loadedImage1);
                    loadedImages[i].print(font, 220, 400, header, 500);
                    loadedImages[i].print(font, 220, 440, venue, 500);

                    loadedImages[i].print(font, 220, 460, location, 500);
                    loadedImages[i].print(font, 220, 520, receipentName, 500);
                    loadedImages[i].print(font, 220, 580, message, 500);
                    //loadedImage.print(font, 10, 30, imageCaption);
                    var path= basePath + '/invitation/' + bookingId + '/' + bookingId+i + '.jpg';
                    //let fullPath = config.BASE_URL + '/uploads/invitation/' + bookingId + '/' + bookingId+i + '.jpg';
                    paths[i] = path;
                    loadedImages[i].write(path);
                }
                res.status(200).json({
                    data: {
                        paths: paths,
                        path: path, //config.BASE_URL + '/uploads/invitation/' + bookingId + '/' + bookingId + '.jpg',
                        bookingData: detail,
                        restaurantDetails: restaurantDetails
                    },
                    status: 1,
                    msg: "Notes data"
                });

            })
            .catch(function (err) {
                console.error(err);
            });

Instead of

loadedImages[i]= Object.assign(loadedImage1);

Use

loadedImages[i]= loadedImage1.clone();

The correct syntax for Object.assign is:

Object.assign(target, ...sources)

In your case the correct usage would be:

loadedImages[i]= Object.assign({}, loadedImage1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM