Created
May 27, 2020 16:41
-
-
Save sahilpaudel/a3e775676e80b403d473f6894f22b6dc to your computer and use it in GitHub Desktop.
Watermark Using Canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createCanvas, loadImage } = require('canvas'); | |
const drawWatermarkAsBuffer = (pathOfSourceImageFile, pathOfWaterMarkImageFile, width, height) => { | |
const canvas = createCanvas(width, height); | |
const ctx = canvas.getContext('2d'); | |
// loading the source image file | |
return loadImage(pathOfSourceImageFile).then(loadSourceImage => { | |
// drawing the source file into canvas | |
ctx.drawImage(loadSourceImage, 0, 0, width, height); | |
// loading the watermark image file | |
return loadImage(pathOfWaterMarkImageFile).then(loadWatermarkImage => { | |
// drawing the watermark image over the source image | |
ctx.drawImage(loadWatermarkImage, 0, 0, width, height); | |
return canvas.toBuffer(); | |
}); | |
}); | |
} | |
module.exports = { | |
drawWatermarkAsBuffer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you hahah, berry useful
I hardcoded a few things, and lowered opacity for the watermark.