How to add watermark to an image in nodejs. I am using loopback framework and I want to add a watermark to every image uploaded,I tried a couple of image processing modules but couldnt implement a watermark.
I tried the image-watermark library with the code :
watermark.embedWatermark('./server/storage/images/img_hkd.jpg', { 'text': 'sample watermark' });
But I am getting this error : Error: spawn identify ENOENT
Instead of watermark use jimp module. It worked for me.
let imgActive = 'active/image.jpg';
Jimp.read('raw/originalimage.png')
.then((tpl) => tpl.clone().write(imgActive))
.then(() => Jimp.read(imgActive))
.then((tpl) =>
Jimp.read('raw/logo.png').then((logoTpl) => {
logoTpl.opacity(0.2)
return tpl.composite(logoTpl, 512, 512, [Jimp.BLEND_DESTINATION_OVER])
}),
)
.then((tpl) => tpl.write('raw/watermark.png'))
}
Just use a package, I've used imaginary without any issue.
//Install
npm install imaginary --save
//Import
var fs = require('fs')
var imaginary = require('imaginary')
var serverUrl = 'localhost:8080'
imaginary('myImage.jpg')
.server(serverUrl)
.watermark({ text: 'copyright' })
.on('error', function (err) {
console.error('Cannot resize the image:', err)
})
.pipe(fs.createWriteStream('markedImage.jpg'))
https://github.com/h2non/node-imaginary
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With