image-placeholder-generator
v2.0.0
Published
A tool to generate base64 image placeholders.
Maintainers
Readme
Image Placeholder Generator
A simple Node.js package to generate base64-encoded image placeholders from an image file. Perfect for loading image placeholders in web applications.
Features
- Generate a base64-encoded placeholder image from an input image
- Supports multiple image formats (JPEG, PNG, WebP)
- Configurable placeholder dimensions and blur intensity
- Comprehensive error handling and input validation
- Easy to use as a CLI tool or library
- TypeScript support with full type definitions
- High test coverage with comprehensive test suite
Installation
To use the image-placeholder-generator in your project, you can install it via npm:
npm install image-placeholder-generator Usage
As a Library You can use this package programmatically in your Node.js application.
JavaScript
const { generatePlaceholder } = require('image-placeholder-generator');
// Generate a base64 placeholder from an image with default options
generatePlaceholder('./path/to/your/image.jpg')
.then((placeholder) => {
console.log('Generated base64 placeholder:', placeholder);
})
.catch((error) => {
console.error('Error generating placeholder:', error);
});
// Generate a placeholder with custom options
const options = {
width: 20,
height: 15,
blur: 2,
format: 'png'
};
generatePlaceholder('./path/to/your/image.jpg', options)
.then((placeholder) => {
console.log('Generated PNG placeholder:', placeholder);
})
.catch((error) => {
console.error('Error generating placeholder:', error);
});TypeScript
import { generatePlaceholder, PlaceholderOptions } from 'image-placeholder-generator';
const options: PlaceholderOptions = {
width: 20,
height: 15,
blur: 2,
format: 'webp'
};
const placeholder = await generatePlaceholder('./path/to/your/image.jpg', options);As a CLI Tool If you installed the package globally, you can use it from the command line.
- Install the package globally:
npm install -g image-placeholder-generator- Run the CLI tool with various options:
# Basic usage
generate-placeholder ./path/to/your/image.jpg
# With custom dimensions and format
generate-placeholder ./path/to/your/image.jpg --width 20 --height 15 --format png
# Output as JSON with metadata
generate-placeholder ./path/to/your/image.jpg --format webp --output json
# Show help
generate-placeholder --helpThis will output the base64-encoded placeholder string or JSON object.
Testing
To run the tests for this package, use Jest. First, make sure Jest is installed:
npm install --save-dev jestThen, run the tests:
npm testContributing
We welcome contributions! If you'd like to improve the package, feel free to fork the repository and submit a pull request.
Steps for Contributing:
- Fork this repository.
- Clone your fork to your local machine.
- Make your changes and test them.
- Submit a pull request to the main repository.
License
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.
If you encounter any issues or have suggestions, feel free to open an issue.
Breakdown of the README Sections:
- Title: Briefly describes the purpose of the package.
- Features: A quick overview of what your package does.
- Installation: Instructions for installing the package.
- Usage: Example code for using the package both as a library and CLI tool.
- Testing: Instructions for running tests with Jest.
- Contributing: Basic guidelines for contributing to the project.
- License: Licensing information.
Let me know if you want to customize it further!
