Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add watermark to an image nodejs

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

like image 337
nikhil.g777 Avatar asked Nov 25 '25 12:11

nikhil.g777


2 Answers

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'))
    }
like image 115
SANTHOSH KUMAR G Avatar answered Nov 28 '25 02:11

SANTHOSH KUMAR G


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

like image 33
Proximo Avatar answered Nov 28 '25 00:11

Proximo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!