@stack-soumik/watermarkimage
v1.0.1
Published
A simple tool to process images with a watermark using Sharp
Downloads
127
Maintainers
Readme
Usage
Basic Watermarking
To add a watermark to an image and save it in the default output directory, use the following code:
javascript
Copy code
import { processImageWithWatermark } from '@stack-soumik/waterMarkImage';
const inputPath = './path/to/your/image.jpg'; // Path to your input image
const watermarkPath = './path/to/your/watermark.png'; // Path to your watermark
processImageWithWatermark(inputPath, watermarkPath)
.then(result => {
console.log('Watermarked image saved at:', result.outputPath);
})
.catch(error => {
console.error('Error processing image:', error);
});
Custom Output Directory
You can also specify a custom directory for the output files:
javascript
Copy code
const customOutputDir = './path/to/custom/output/';
processImageWithWatermark(inputPath, watermarkPath, customOutputDir)
.then(result => {
console.log('Watermarked image saved at:', result.outputPath);
})
.catch(error => {
console.error('Error processing image:', error);
});
API Reference
processImageWithWatermark(inputPath: string, watermarkPath: string, outputPath?: string)
- inputPath:
string
- The path to the input image that you want to watermark. - watermarkPath:
string
- The path to the watermark image you wish to overlay on the input image. - outputPath:
string
(Optional) - The path to the directory where the processed image will be saved. If not provided, the default output directory will be used.
Returns: A Promise that resolves with an object containing:
outputPath
: The path where the watermarked image is saved.fileName
: The name of the saved file.
Example:
javascript
Copy code
const result = await processImageWithWatermark('./image.jpg', './watermark.png');
console.log(result.outputPath); // Outputs: path/to/default/output/image-watermarked.jpg
Error Handling
The package is designed to handle common errors, such as missing files or invalid paths. If an error occurs, it will reject the promise with an appropriate error message.
Example:
javascript
Copy code
try {
await processImageWithWatermark('invalid.jpg', watermarkPath);
} catch (error) {
console.error('Failed to process image:', error.message); // Will log the error message
}
Examples
1. Basic Watermarking
javascript
Copy code
import { processImageWithWatermark } from '@stack-soumik/waterMarkImage';
(async () => {
const inputPath = './image.jpg';
const watermarkPath = './watermark.png';
try {
const result = await processImageWithWatermark(inputPath, watermarkPath);
console.log('Watermarked image saved at:', result.outputPath);
} catch (error) {
console.error('Error:', error.message);
}
})();
2. Custom Output Directory
javascript
Copy code
import { processImageWithWatermark } from '@stack-soumik/waterMarkImage';
(async () => {
const inputPath = './image.jpg';
const watermarkPath = './watermark.png';
const customOutputDir = './custom-output/';
try {
const result = await processImageWithWatermark(inputPath, watermarkPath, customOutputDir);
console.log('Watermarked image saved at:', result.outputPath);
} catch (error) {
console.error('Error:', error.message);
}
})();
Contributing
We welcome contributions to improve this package! Here’s how you can help:
Fork the repository on GitHub.
Create a new branch for your feature or bug fix:
bash Copy code git checkout -b feature/your-feature-name
Make your changes and commit them:
bash Copy code git commit -m 'Add some feature'
Push to your branch:
bash Copy code git push origin feature/your-feature-name
Open a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contact
For any questions or feedback, feel free to reach out:
- Author: Soumik Sarkar
- Email: [email protected]