0.1.0 • Published 11 months ago

image-down v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

image-down

npm license size GitHub release

Yet another CLI tool to batch compress / downscale images.

:green_book: Quick Start

npx image-down images/* --width 800 --output compressed
import { compressImages } from 'image-down';

// Wildcards are not supported in library usages
await compressImages(['./images/image1.jpg'], {
  width: 800,
  outputDir: 'compressed',
});

:wrench: Cli

Usage: npx image-down [options]

Commands:
  help     Display help
  version  Display version

Options:
  -f, --format      Convert images to a format.
  -h, --height      Resize images to a certain height.
  -H, --help        Output usage information
  -o, --output      Specify the output directory, default to '.'.
  -p, --percentage  Resize images according to the width by percentage.
  -s, --suffix      Adding a suffix to the output filename.
  -v, --version     Output the version number
  -w, --width       Resize images to a certain width.

Examples:
  - Compressing all files from folder images to jpg with widths of 800px and add '-min' to converted filenames, saving all compressed images to folder compressed.
  $ npx image-down images/* --width 800 --format jpg --suffix min --output compressed

:book: Library

await compressImages(pathsArray, options);

Options

NameTypeDescription
percentagenumberResize images according to the width by percentage.
widthnumberResize images to a certain width.
heightnumberResize images to a certain height.
formatnumberConvert images to a format.
outputDirstringSpecify the output directory, will not output if not defined.
outputFilenameSuffixstringAdding a suffix to the output filename.
returnBuffersbooleanReturning all converted buffers with corresponding file paths.
onProgressFunctionA function that is called when each file is processed.

onProgress

NameTypeDescription
filePathstringThe original path to the file.
fileBufferBufferThe converted file buffer.
progressObjectThe progress object.
progress.queueIndexnumberFile index.
progress.totalnumberQueue length.
progress.statusstring"success" or "failed.
progress.filenamestringThe name of the output file.

Completed Example

/**
 * Compressing all files from folder images to jpg with widths of 800px
 * and add '-min' to converted filenames,
 * saving all compressed images to folder compressed.
 */

import { glob } from 'glob';
import { compressImages } from 'image-down';

const filePaths = await glob('images/*');
await compressImages(filePaths, {
  width: 800,
  format: 'jpg',
  suffix: 'min',
  output: './compressed',
  onProgress({ progress }) {
    console.log(`Saved ${progress.filename}.`);
  },
});