Skip to content

An NPM package to effortlessly add watermarks to your images

License

Notifications You must be signed in to change notification settings

zahid47/sharp-watermark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sharp-watermark

npm type definitions npm npm

Installation

npm install sharp-watermark

Usage

Add an image watermark:

import { addImageWatermark } from "sharp-watermark";

const watermarkedImage = await addImageWatermark(
  "path/mainImage.jpg", // or a buffer
  "path/watermark.jpg", // or a buffer
  options
);

Add a text watermark:

import { addTextWatermark } from "sharp-watermark";

const watermarkedImage = await addTextWatermark(
  "path/mainImage.jpg", // or a buffer
  "Hello World!",
  options
);

Parameters

  • 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();

Options

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 both x & y property is passed.
  • x (default: undefined): the x coordinate of the watermark image.
  • y (default: undefined): the y coordinate of the watermark image.

Special Thanks

  • 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.

About

An NPM package to effortlessly add watermarks to your images

Topics

Resources

License

Stars

Watchers

Forks