1.0.5 • Published 7 months ago
smart-image-compressor v1.0.5
Smart Image Compressor
Smart Image Compressor is an NPM package that allows users to compress images efficiently using XML-based input. It supports multiple formats (JPEG, PNG, WebP, GIF, TIFF) and can process images from URLs, local files, or entire directories.
Features
- ✅ Supports image compression from URLs, local files, and directories
- ✅ Supports multiple formats: JPEG, PNG, WebP, GIF, TIFF
- ✅ Allows custom quality settings, resizing options (width & height)
- ✅ Uses XML-based input for structured configuration
- ✅ Creates an output directory for processed images
- ✅ Implements error handling and logging
Installation
Dependencies
This package requires the following dependencies:
npm install sharp fs axios path xml2js
Then, install the Smart Image Compressor package:
npm install smart-image-compressor
Usage
1️⃣ Compressing an Image from a URL
const compressImageXML = require('smart-image-compressor');
const xmlInput = `
<compressImage>
<img>https://www.example.com/sample.jpg</img>
{ "quality": 75, "format": "jpeg", "width": 800 }
</compressImage>
`;
compressImageXML(xmlInput)
.then(() => console.log('Compression complete!'))
.catch(err => console.error('Error:', err));
2️⃣ Compressing a Local Image File
const xmlInput = `
<compressImage>
<img>/path/to/image.jpg</img>
{ "quality": 80, "format": "png", "width": 600 }
</compressImage>
`;
compressImageXML(xmlInput);
3️⃣ Compressing All Images in a Directory
const xmlInput = `
<compressImage>
<img>/path/to/images</img>
{ "quality": 70, "format": "webp", "width": 500 }
</compressImage>
`;
compressImageXML(xmlInput);
How It Works
- Parses the XML input to extract the image path and compression options.
- If the input is a URL, downloads the image before processing.
- Compresses images based on user-defined settings (quality, format, resizing).
- Saves the compressed image to the
compressed_images
directory. - Logs errors in case of invalid input or processing issues.
Supported Formats
- JPEG / JPG
- PNG
- WebP
- GIF
- TIFF
Configuration Options
Option | Type | Default | Description |
---|---|---|---|
quality | Number | 80 | Compression quality (1-100) |
format | String | "jpeg" | Output format (jpeg, png, webp, gif, tiff) |
width | Number | null | Resize width (optional) |
height | Number | null | Resize height (optional) |
Error Handling
- Invalid XML Input: If the XML structure is incorrect, an error will be thrown.
- Unsupported Format: If an unsupported format is specified, an error will be logged.
- File Not Found: If the specified image or directory does not exist, an error message will be shown.
- Network Issues: If an image URL fails to download, an error message will be displayed.
License
MIT License