0.0.8 • Published 6 months ago

images-watermark v0.0.8

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

Images Watermark

images-watermark is a Node.js library that uses the Sharp library to add watermarks (text or logos) to images. The tool allows you to watermark images to protect or brand your content. You can apply text watermarks, logo watermarks, or a combination of both.

npm version License

Features

  • Text Watermark: Add custom text as a watermark to images.
  • Image Logo Watermark: Overlay an image logo as a watermark.
  • Combined Watermarks: Apply both text and image watermarks on the same image.

Installation

You can install the library using npm:

  npm install images-watermark

Watermarking an Image Using singleImageWatermark

This guide demonstrates how to add a watermark to an image using the singleImageWatermark library. This is useful for branding or protecting your images.

Code Example

const { singleImageWatermark } = require('images-watermark');
const path = require('path');

const imagesWatermark = async function (req, res) {
    try {
        const watermarkedImage = await singleImageWatermark({
            // Path to the image you want to watermark
            imagePath: path.join(__dirname, '../../../public/images/image.jpg'), 

            // Path to the watermark image (e.g., logo or watermark text)
            watermarkPath: path.join(__dirname, '../../../public/images/Watermark.png'), 

            // Allowed referrers for cross-origin resource sharing (CORS)
            allowedReferrers: ['http://localhost:3000'], 

            // Custom headers to mimic the request context
            headers: {
                host: 'localhost:3000', // Specify the host for the request
                connection: 'keep-alive', // Keep the connection open for further requests
                'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
                // Include the User-Agent header to emulate the client browser
            },
        });

        // Set the response content type and send the watermarked image
        res.setHeader('Content-Type', 'image/png');
        return res.send(watermarkedImage);
    } catch (error) {
        console.error('Error generating watermarked image:', error);
        res.status(500).send('An error occurred while processing the image.');
    }
};

Parameters

ParameterTypeMandatoryDescription
imagePathstringRequiredThe path to the original image.
headersobjectRequiredHeaders for API calls, e.g., request headers.
allowedReferrersarrayRequiredList of allowed domains for cross-platform access (CORS).
watermarkPathstringOptionalThe path to the watermark image (e.g., a logo).
textWatermarkstringOptionalText to use as a watermark.
appNamestringOptionalThe application name (for branding purposes).
textColorstringOptionalColor of the watermark text, e.g., #000000.
opacitystringOptionalOpacity of the watermark text, e.g., 0.3.
fontWeightstringOptionalFont weight of the watermark text, e.g., 800.
textLineSpacingintOptionalLine spacing for the text watermark.
fontFamilystringOptionalFont family for the text watermark, e.g., Inter, Arial, sans-serif.

Watermarking Multiple Images Using multiImageWatermark

This guide demonstrates how to add a watermark to multiple images using the multiImageWatermark library. This is useful for branding or protecting your images.

Code Example

const { multiImageWatermark } = require('images-watermark');
const path = require('path');

const imagesWatermark = async function (req, res) {
    try {
        const watermarkedImages = await multiImageWatermark({
            // Path to the images you want to watermark
            imagePaths: [path.join(__dirname, '../../../public/images/image1.jpg'), path.join(__dirname, '../../../public/images/image2.jpg')], 

            // Path to the watermark image (e.g., logo or watermark text)
            watermarkPath: path.join(__dirname, '../../../public/images/Watermark.png'), 

            // Allowed referrers for cross-origin resource sharing (CORS)
            allowedReferrers: ['http://localhost:3000'], 

            // Custom headers to mimic the request context
            headers: {
                host: 'localhost:3000', // Specify the host for the request
                connection: 'keep-alive', // Keep the connection open for further requests
                'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
                // Include the User-Agent header to emulate the client browser
            },
        });

        return res.send(watermarkedImages);
    } catch (error) {
        console.error('Error generating watermarked image:', error);
        res.status(500).send('An error occurred while processing the image.');
    }
};

Parameters

ParameterTypeMandatoryDescription
imagePathsarrayRequiredIn array, push paths to the original images.
headersobjectRequiredHeaders for API calls, e.g., request headers.
allowedReferrersarrayRequiredList of allowed domains for cross-platform access (CORS).
watermarkPathstringOptionalThe path to the watermark image (e.g., a logo).
textWatermarkstringOptionalText to use as a watermark.
appNamestringOptionalThe application name (for branding purposes).
textColorstringOptionalColor of the watermark text, e.g., #000000.
opacitystringOptionalOpacity of the watermark text, e.g., 0.3.
fontWeightstringOptionalFont weight of the watermark text, e.g., 800.
textLineSpacingintOptionalLine spacing for the text watermark.
fontFamilystringOptionalFont family for the text watermark, e.g., Inter, Arial, sans-serif.

Example Usage

This implementation watermarks an image (or multiple images) with a logo or text and allows customization of the watermark's appearance. It also ensures proper CORS handling for cross-domain requests.

🔗 Author Details

portfolio linkedin

0.0.8

6 months ago

0.0.7

6 months ago

0.0.6

6 months ago

0.0.5

6 months ago

0.0.4

6 months ago

0.0.3

6 months ago

0.0.2

6 months ago

0.0.0

6 months ago

0.0.1

6 months ago

1.0.17

6 months ago

1.0.16

6 months ago

1.0.15

6 months ago

1.0.14

6 months ago

1.0.13

6 months ago

1.0.12

6 months ago

1.0.11

6 months ago

1.0.10

6 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.51

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago