Skip to content

Latest commit

Β 

History

37 Commits

Folders and files

Repository files navigation

πŸ–ΌοΈ Auto Image Converter

Automatically convert and resize images to modern formats (WebP, AVIF, PNG, JPEG, TIFF) with real-time file watching and flexible resize options.

v3.0.0 β€” breaking config/path fixes vs 2.2.x. See Breaking changes and CHANGELOG.md.

πŸš€ Features

πŸ”„ Convert images to WebP, AVIF, PNG, JPEG, or TIFF

πŸ“ Resize inside convert (needResize) and/or via a separate resize command (same format, dimensions only)

πŸ’Ύ needResizeOriginal β€” also save the resized file in the source format

πŸ“ Per-command paths β€” separate dir / removeOriginal / outputDir for convert and resize

πŸ‘€ Watch convert + watch resize β€” process new files as they arrive (FTP/drop folders friendly)

⚑ Parallel workers via concurrency

πŸ—‚οΈ flat / mirror output layout

βš™οΈ Easy configuration via image-converter.config.mjs (fully commented sample in the repo)

πŸ“¦ Installation

npm install auto-image-converter --save-dev

Or develop in this repo:

npm install
npm run convert
npm run watch
npm run resize
npm run resize:watch

Or use locally via npm link:

cd auto-image-converter
npm link
cd ../your-project
npm link auto-image-converter

βš™οΈ Configuration

Create image-converter.config.mjs in the project root (same folder you run npm scripts from β€” cwd):

export default {
  // Shared
  recursive: true,       // walk subfolders under each command's dir
  concurrency: 4,        // parallel workers
  ignoreOnStart: true,   // watch: skip files that already exist on startup

  // --- convert + watch (auto-convert-images / auto-convert-images-watch) ---
  convertation: {
    dir: "./public/original",          // where to look for sources (cwd-relative or absolute)
    removeOriginal: true,              // delete source from convertation.dir after success
    converted: "png,jpg,jpeg,tiff",    // source formats (comma-separated)
    format: "webp",                    // output codec: webp | avif | png | jpg | jpeg | tiff
    quality: 80,                       // 0–100
    outputDir: "./public/converted",   // null = next to source
    outputDirMode: "flat",             // flat | mirror (mirror keeps subfolders from dir)

    needResize: true,                  // resize once, then encode β†’ outputDir
    needResizeOriginal: false,         // also write resized SOURCE format β†’ resize.outputDir
  },

  // --- resize geometry + resize / resize:watch (no format change) ---
  resize: {
    dir: "./public/converted",         // scan folder for resize CLI / resize:watch
    removeOriginal: false,             // independent from convertation.removeOriginal
    width: 1920,                       // or null if only height
    height: null,                      // or null if only width
    fit: "cover",                      // cover | contain | fill | inside | outside
    position: "center",
    withoutEnlargement: true,          // don't upscale smaller images

    targetFormat: "webp",              // which files to resize: "webp" | "png,jpg" | null β†’ convertation.format
    outputDir: "./public/resized",     // null + removeOriginal true β†’ overwrite; false β†’ -1920w suffix
    outputDirMode: "flat",
  },
};

The same file in this repo has longer English comments on every field.

Who owns what

convert / watch resize / resize:watch
Scan folder convertation.dir resize.dir
Delete source convertation.removeOriginal resize.removeOriginal
Output folder convertation.outputDir resize.outputDir
Format change yes β€” convertation.format no β€” same extension
Which files convertation.converted resize.targetFormat

Top-level dir / removeOriginal / needResize still work with a deprecation warning; prefer per-section fields.

Paths

Value Meaning
Relative (./public/original) From cwd
Absolute Used as-is
outputDir: null Next to the source file

On Windows prefer ./public/.... A path like /public is absolute from the drive root.

outputDirMode

  • flat (default) β€” original/tests/cat.png β†’ converted/cat.webp
  • mirror β€” keep subfolders from dir β†’ converted/tests/cat.webp

Input formats

Same style everywhere β€” comma-separated extensions (no *.{…} required):

converted: "png"
converted: "png,jpg,jpeg,tiff"

targetFormat: "webp"
targetFormat: "png,jpg,webp"
targetFormat: null // β†’ convertation.format

Legacy *.{png,jpg} still works if you already have it in a config.

Resize in the convert pipeline

  1. needResize: true β€” resize once in memory, then encode to format β†’ convertation.outputDir
  2. needResizeOriginal: true β€” also write resized source-format file β†’ resize.outputDir
    (requires needResize + resize.outputDir)

⚠️ Don’t also run resize:watch on the same files if convert already resized them β€” you’ll resize twice.

Resize options

  • By width only: width: 1920, height: null
  • By height only: width: null, height: 1080
  • To a box: width: 1920, height: 1080 (uses fit)

withoutEnlargement: true β€” never upscale; only shrink larger images.

Fit modes

  • cover β€” fill size, crop excess (default)
  • contain β€” fit inside, may pad
  • fill β€” stretch
  • inside β€” fit inside, no enlarge
  • outside β€” cover size, may enlarge

πŸ› οΈ Usage

Commands

# One-time convert (+ optional needResize in the same pass)
npx auto-convert-images
npm run convert

# Watch convert β€” convertation.dir
npx auto-convert-images-watch
npm run watch

# One-time resize (same format, dimensions only)
npx auto-convert-images-resize
npm run resize

# Watch resize β€” resize.dir
npx auto-convert-images-resize-watch
npm run resize:watch

Package.json scripts

{
  "scripts": {
    "convert": "auto-convert-images",
    "watch": "auto-convert-images-watch",
    "resize": "auto-convert-images-resize",
    "resize:watch": "auto-convert-images-resize-watch"
  }
}

With Next.js

npm install concurrently --save-dev
{
  "scripts": {
    "dev": "concurrently \"npm run watch\" \"next dev\""
  }
}

Add resize:watch only if you use a separate resize stage.

πŸ“‹ Use cases

1. One pass β€” resize + convert

convertation.dir = ./public/original
convertation.outputDir = ./public/converted
convertation.needResize = true
npm run convert   # or npm run watch

2. Watch drop folder / FTP

FTP β†’ convertation.dir
npm run watch

Watch waits for a stable file size before processing.

3. Convert first, resize later

# convert with needResize: false
convertation.dir β†’ convertation.outputDir

# then
resize.dir = ./public/converted
resize.targetFormat = "webp"
resize.outputDir = ./public/resized
npm run resize   # or npm run resize:watch
  • resize.removeOriginal: false + outputDir: null β†’ size suffix (image-1920w.webp)
  • resize.removeOriginal: true + outputDir: null β†’ overwrite source

4. Resize only (no convert)

resize.dir = ./public/original
resize.targetFormat = "png,jpg"
npm run resize

πŸ—οΈ Architecture

  • Pipeline β€” queue + workers (convert or resize mode)
  • ResizeImages / ConvertImages β€” Sharp wrappers
  • FileManager β€” path resolution (flat / mirror, cwd-relative)
  • globPattern β€” strict *.{…} parsing

πŸ’₯ Breaking changes in 3.0

  • Relative outputDir / dir resolve from cwd (2.2.x relative outputDir was from the source file folder)
  • Absolute outputDir is flat (2.2.x added a per-file name subfolder)
  • Prefer convertation.dir / resize.dir and per-section removeOriginal
  • needResize / needResizeOriginal under convertation
  • Prefer converted / targetFormat as "png,jpg" (legacy *.{…} still ok)
  • Resize CLI uses resize.dir + targetFormat (still no format conversion β€” same as 2.2.x intent)
  • New: auto-convert-images-resize-watch

Full details: CHANGELOG.md.

πŸ› Bug reports

Report issues on GitHub Issues. Include description, steps, expected/actual behavior, config, Node.js version, and OS.

πŸ“„ License

MIT β€” free to use, modify, and distribute with proper attribution.

Releases

Packages

Used by

Contributors

Languages