Add Watermark To Images In NodeJS (Simple Examples)

Welcome to a tutorial on how to watermark images in NodeJS. So you need to add a watermark to images in NodeJS? In Javascript, we can use the Canvas API to do just that. Thankfully in NodeJS, we also have a Canvas library to emulate the native API – Read on for the examples!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

NODEJS WATERMARK IMAGES

All right, let us now get into the examples of adding a watermark to images in NodeJS.

 

QUICK SETUP

Run npm i canvas to install the required modules.

 

1) SIMPLE TEXT WATERMARK

1-text.js
// (A) LOAD MODULES
const fs = require("fs"),
      { registerFont, createCanvas, loadImage } = require("canvas");
 
// (B) SETTINGS - CHANGE FONT TO YOUR OWN
var
sFile = "demo.png",  // source image
sSave = "demoA.png", // "save as"
sText = "COPYRIGHT", // copywrite text
sX = 10, sY = 40;    // text position
registerFont("C:/Windows/Fonts/arialbd.ttf", { family: "Arial Bold" });
 
// (C) LOAD IMAGE + DRAW TEXT
loadImage(sFile).then(img => {
  // (C1) CREATE CANVAS
  const canvas = createCanvas(img.width, img.height),
        ctx = canvas.getContext("2d");
 
  // (C2) DRAW IMAGE ONTO CANVAS
  ctx.drawImage(img, 0, 0);
 
  // (C3) WRITE COPYRIGHT TEXT ONTO IMAGE
  ctx.font = '36px "Arial Bold"';
  ctx.fillStyle = "rgba(255, 255, 255, 0.7)";
  ctx.fillText(sText, sX, sY);
 
  // (C4) SAVE
  const out = fs.createWriteStream(sSave),
        stream = canvas.createPNGStream();
  stream.pipe(out);
  out.on("finish", () => console.log("Done"));
});

A quick walkthrough for those who are lost:

  • (C) loadImage("SOURCE").then(...) Make sure that the image is fully loaded before we add the watermark.
  • (C1) createCanvas(WIDTH, HEIGHT) Create an empty canvas.
  • (C2) drawImage(IMAGE, X, Y) Draw the source image onto the canvas.
  • (C3) fillText(TEXT, X, Y) Draw the text watermark onto the canvas.
  • (C4) Save the canvas to a file.

 

 

2) IMAGE WATERMARK

2-image.js
// (A) LOAD MODULES
const fs = require("fs"),
      { createCanvas, loadImage } = require("canvas");
 
// (B) SETTINGS
var
sFile = "demo.png",   // source image
sMark = "watermark.png", // watermark image
sSave = "demoB.png",  // "save as"
sX = 10, sY = 10,     // watermark position
ready = 0;            // flag to track image load
 
// (C) ADD WATERMARK IMAGE
function mark () { if (ready==2) {
  // (C1) CREATE CANVAS
  const canvas = createCanvas(sFile.width, sFile.height),
        ctx = canvas.getContext("2d");
 
  // (C2) DRAW SOURCE IMAGE ONTO CANVAS
  ctx.drawImage(sFile, 0, 0);
 
  // (C3) DRAW WATERMARK IMAGE ONTO CANVAS
  ctx.drawImage(sMark, sX, sY);
 
  // (C4) SAVE
  const out = fs.createWriteStream(sSave),
  stream = canvas.createPNGStream();
  stream.pipe(out);
  out.on("finish", () => console.log("Done"));
}}
 
// (D) LOAD SOURCE + WATERMARK IMAGES
loadImage(sFile).then(img => {
  sFile = img; ready++; mark();
});
loadImage(sMark).then(img => {
  sMark = img; ready++; mark();
});

Keep calm and look carefully – This is pretty much the same as the text watermark. But we “copy” the source image onto the canvas, followed by “pasting” the image watermark on top.

P.S. For the uninitiated, make sure that the watermark image has a transparent background; Only PNG GIF WEBP files support alpha channels (transparency).

 

 

3) CENTER IMAGE WATERMARK

3-center.js
// (A) LOAD MODULES
const fs = require("fs"),
      { createCanvas, loadImage } = require("canvas");
 
// (B) SETTINGS
var
sFile = "demo.png",   // source image
sMark = "watermark.png", // watermark image
sSave = "demoC.png",  // "save as"
ready = 0;            // flag to track image load
 
// (C) ADD WATERMARK IMAGE
function mark () { if (ready==2) {
  // (C1) CREATE CANVAS
  const canvas = createCanvas(sFile.width, sFile.height),
        ctx = canvas.getContext("2d");
 
  // (C2) CALCULATE CENTER COORDINATES
  var sX = Math.floor(sFile.naturalWidth/2 - sMark.naturalWidth/2),
      sY = Math.floor(sFile.naturalHeight/2 - sMark.naturalHeight/2);
 
  // (C3) DRAW SOURCE + WATERMARK IMAGES
  ctx.drawImage(sFile, 0, 0);
  ctx.drawImage(sMark, sX, sY);
 
  // (C4) SAVE
  const out = fs.createWriteStream(sSave),
  stream = canvas.createPNGStream();
  stream.pipe(out);
  out.on("finish", () => console.log("Done"));
}}
 
// (D) LOAD SOURCE + WATERMARK IMAGES
loadImage(sFile).then(img => {
  sFile = img; ready++; mark();
});
loadImage(sMark).then(img => {
  sMark = img; ready++; mark();
});

Finally, centering the watermark unfortunately involves some Math. We need to calculate the center coordinates of where to place the watermark.

  • X = FLOOR((IMAGE WIDTH - WATERMARK WIDTH) ÷ 2)
  • Y = FLOOR((IMAGE HEIGHT - WATERMARK HEIGHT) ÷ 2)

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *