Skip to content

yinyanfr/image-down

Repository files navigation

image-down

npm license size GitHub release

Yet another CLI tool to batch compress / downscale images.

📗 Quick Start

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

// For the wildcard example, please refer to the complete example below
await compressImages(['./images/image1.jpg'], {
  width: 800,
  outputDir: 'compressed',
});

🔧 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

📖 Library

await compressImages(pathsArray, options);

Options

Name Type Description
percentage number Resize images according to the width by percentage.
width number Resize images to a certain width.
height number Resize images to a certain height.
format number Convert images to a format.
outputDir string Specify the output directory, will not output if not defined.
outputFilenameSuffix string Adding a suffix to the output filename.
returnBuffers boolean Returning all converted buffers with corresponding file paths.
onProgress Function A function that is called when each file is processed.

onProgress

Name Type Description
filePath string The original path to the file.
fileBuffer Buffer The converted file buffer.
progress Object The progress object.
progress.queueIndex number File index.
progress.total number Queue length.
progress.status string "success" or "failed".
progress.filename string The name of the output file.

Complete 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}.`);
  },
});

compressImage

You can use compressImage if you only want to compress 1 image.

import { compressImage } from 'image-down';

const compressedBuffer = await compressImage('./image/image1.jpg', {
  percentage: 50,
  returnBuffers: true,
});

Please note that compressImage returns directly the Buffer when options.returnBuffers is set to true.