Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 8, 2023 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/e76018e6f1ccf4508d3459bc91e02992 to your computer and use it in GitHub Desktop.
Save code-boxx/e76018e6f1ccf4508d3459bc91e02992 to your computer and use it in GitHub Desktop.
NodeJS Add Watermark To Image

NODEJS ADD WATERMARK TO IMAGE

https://code-boxx.com/watermark-images-nodejs/

NOTES

  1. Change registerFont(FONT) to your own in 1-text.js
  2. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Save below images as demo.png and potato.png.
    • Install required modules - npm i canvas
    • Run all the examples.

IMAGES

demo watermark

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// (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" }); // CHANGE TO YOUR OWN!
// (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) 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();
});
// (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();
});
curl https://user-images.githubusercontent.com/11156244/281441200-efe3fbec-c7e1-4e9c-aadf-94322772402d.png --ssl-no-revoke --output demo.png
curl https://user-images.githubusercontent.com/11156244/281441217-92ff2b16-66ff-476e-a4aa-157fdb60d64e.png --ssl-no-revoke --output watermark.png
call npm i canvas
node 1-text.js
node 2-image.js
node 3-center.js
curl https://user-images.githubusercontent.com/11156244/281441200-efe3fbec-c7e1-4e9c-aadf-94322772402d.png --ssl-no-revoke --output ./demo.png
curl https://user-images.githubusercontent.com/11156244/281441217-92ff2b16-66ff-476e-a4aa-157fdb60d64e.png --ssl-no-revoke --output ./watermark.png
source "npm i canvas"
node ./1-text.js
node ./2-image.js
node ./3-center.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment