1.0.2 • Published 1 year ago

@codext/project-image-compression v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

project-image-compression

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.

Features

  • šŸ–¼ļø 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

Installation

npm install @codext/project-image-compression

Quick Start

  1. Add the script to your package.json:
{
  "scripts": {
    "compress-images": "npx @codext/project-image-compression"
  }
}
  1. 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
}
  1. Run the compression:
npm run compress-images

Configuration

Create an img.config.json file in your project root with the following options:

OptionTypeDefaultDescription
qualityNumber80Output image quality (0-100)
maxWidthNumber1920Maximum width of output images
maxHeightNumber1080Maximum height of output images
convertToWebpBooleanfalseConvert images to WebP format
folderString"./images"Source folder containing images to process
skipExistingBooleantrueSkip already processed images
createBackupBooleantrueCreate backup of original images
outputFolderString|nullnullCustom output directory (null to overwrite originals)

Example Usage

Basic Compression

For basic image compression with default settings:

{
  "folder": "./images",
  "quality": 80
}

Convert to WebP

To compress and convert all images to WebP format:

{
  "folder": "./images",
  "quality": 85,
  "convertToWebp": true,
  "outputFolder": "./compressed"
}

High-Quality Resize

For resizing large images while maintaining high quality:

{
  "folder": "./images",
  "quality": 90,
  "maxWidth": 1920,
  "maxHeight": 1080,
  "createBackup": true
}

CLI Output

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%

Error Handling

  • 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

Requirements

  • Node.js 12.0 or higher
  • NPM or Yarn package manager

Contributing

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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Credits

Built with:

  • Sharp - High-performance image processing
  • Glob - File pattern matching
  • Chalk - Terminal string styling
  • Ora - Terminal spinner
1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago