npm install sharp-watermarkimport { addImageWatermark } from "sharp-watermark";
const watermarkedImage = await addImageWatermark(
"path/mainImage.jpg", // or a buffer
"path/watermark.jpg", // or a buffer
options
);import { addTextWatermark } from "sharp-watermark";
const watermarkedImage = await addTextWatermark(
"path/mainImage.jpg", // or a buffer
"Hello World!",
options
);mainImage(required): The main image to which the watermark will be added. It can be either a string path or a buffer representing the image.watermarkImage(required for image watermarks): The watermark image to be added to the main image. It can be either a string path or a buffer representing the image.watermarkText(required for text watermarks): The text to be used as the watermark.options(optional): an object containing the options for the watermarking process. See the Options section for more details.
Access the watermarked image:
// you can save it to a file
watermarkedImage.toFile("watermarkedImage.png");
// or you can convert it to a buffer
const buffer = await watermarkedImage.toBuffer();The options parameter is an object that contains the following properties:
- ratio (default:
0.4): the ratio of the watermark image to the main image. - dpi (default:
300): Size of the text. Only applicable for text watermarking. - opacity (default:
0.6): the opacity of the watermark text. Only applicable for text watermarking. - position (default:
center): the position of the watermark image. Can be one of the following:topLeft,top,topRight,left,center,right,bottomLeft,bottom,bottomRight. Ignored if bothx&yproperty is passed. - x (default:
undefined): the x coordinate of the watermark image. - y (default:
undefined): the y coordinate of the watermark image.
- sharp - The image processing library used in this project.
- jimp-watermark - The inspiration for this project. Reasons to use this package over jimp-watermark:
- Less file size.
- Support for watermark positioning.
- built in type definitions.