1.0.5 • Published 7 months ago

smart-image-compressor v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

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

  1. Parses the XML input to extract the image path and compression options.
  2. If the input is a URL, downloads the image before processing.
  3. Compresses images based on user-defined settings (quality, format, resizing).
  4. Saves the compressed image to the compressed_images directory.
  5. Logs errors in case of invalid input or processing issues.

Supported Formats

  • JPEG / JPG
  • PNG
  • WebP
  • GIF
  • TIFF

Configuration Options

OptionTypeDefaultDescription
qualityNumber80Compression quality (1-100)
formatString"jpeg"Output format (jpeg, png, webp, gif, tiff)
widthNumbernullResize width (optional)
heightNumbernullResize 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

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago