A powerful and flexible CLI tool for bulk image compression and optimization. This tool helps you automatically compress and resize images in a directory while maintaining quality and optionally converting them to WebP format.
- 🖼️ Bulk image compression with customizable quality settings
↔️ Automatic image resizing while maintaining aspect ratio- 🔄 Optional WebP conversion
- 📁 Custom output directory support
- 💾 Automatic backup creation
- 📊 Detailed compression statistics
- 🎯 Configurable via JSON file
- 🚀 Support for JPG, JPEG, PNG, and WebP formats
npm install @codext/project-image-compression
- Add the script to your package.json:
{
"scripts": {
"compress-images": "npx @codext/project-image-compression"
}
}
- Create an
img.config.json
file in your project root:
{
"quality": 80,
"maxWidth": 1920,
"maxHeight": 1080,
"convertToWebp": false,
"folder": "./images",
"skipExisting": true,
"createBackup": true,
"outputFolder": null
}
- Run the compression:
npm run compress-images
Create an img.config.json
file in your project root with the following options:
Option | Type | Default | Description |
---|---|---|---|
quality | Number | 80 | Output image quality (0-100) |
maxWidth | Number | 1920 | Maximum width of output images |
maxHeight | Number | 1080 | Maximum height of output images |
convertToWebp | Boolean | false | Convert images to WebP format |
folder | String | "./images" | Source folder containing images to process |
skipExisting | Boolean | true | Skip already processed images |
createBackup | Boolean | true | Create backup of original images |
outputFolder | String|null | null | Custom output directory (null to overwrite originals) |
For basic image compression with default settings:
{
"folder": "./images",
"quality": 80
}
To compress and convert all images to WebP format:
{
"folder": "./images",
"quality": 85,
"convertToWebp": true,
"outputFolder": "./compressed"
}
For resizing large images while maintaining high quality:
{
"folder": "./images",
"quality": 90,
"maxWidth": 1920,
"maxHeight": 1080,
"createBackup": true
}
The tool provides detailed statistics after processing:
- Number of images processed
- Total size reduced (in MB)
- Average compression percentage
- Processing status for each image
Example output:
✔ Image compression completed!
Stats:
- Images processed: 24
- Total size reduced: 15.7 MB
- Average compression: 65.3%
- The tool creates backups by default (can be disabled)
- Failed image processing is reported but doesn't stop the batch
- Invalid configuration files trigger helpful error messages
- Missing source directory prompts for creation
- Node.js 12.0 or higher
- NPM or Yarn package manager
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Built with: