Automatically convert and resize images to modern formats (WebP, AVIF, PNG, JPEG, TIFF) with real-time file watching and flexible resize options.
npm install auto-image-converterimage-converter.config.mjs
bash
npm install auto-image-converter --save-dev
`
Or use locally via npm link:
`bash
cd auto-image-converter
npm link
cd ../your-project
npm link auto-image-converter
`
āļø Configuration
Create a image-converter.config.mjs file in the root of your project:
`javascript
export default {
// Main settings
dir: "./public", // Directory to scan for images
removeOriginal: true, // Delete original files after conversion
recursive: true, // Recursive search in subdirectories
ignoreOnStart: true, // Ignore existing files on watcher startup
concurrency: 4, // Number of parallel workers
// Conversion settings
convertation: {
converted: "*.{png,jpg,jpeg,tiff}", // Source file pattern
format: "webp", // Target format: webp, avif, png, jpg, tiff
quality: 80, // Quality (0-100)
outputDir: null, // null = same folder, or path for output
},
// Resize settings (optional)
needResize: true, // Enable resize
resize: {
width: 1920, // Width (or null)
height: null, // Height (or null)
fit: "cover", // cover, contain, fill, inside, outside
position: "center", // Cropping position
withoutEnlargement: true, // Don't enlarge small images
},
};
`
$3
- By width only: width: 1920, height: null - reduces to width, height scales proportionally
- By height only: width: null, height: 1080 - reduces to height, width scales proportionally
- To aspect ratio: width: 1920, height: 1080 - fits/crops to specific aspect ratio
$3
- cover - fills entire size, cropping excess (default)
- contain - fits fully, may add padding
- fill - stretches without preserving aspect ratio
- inside - reduces to fit, doesn't enlarge
- outside - covers entire size, may enlarge
š ļø Usage
$3
`bash
One-time conversion of all files
npx auto-convert-images
Watch mode - processes new files as they're added
npx auto-convert-images-watch
One-time resize of already converted files
npx auto-convert-images-resize
`
$3
Add to your package.json:
`json
{
"scripts": {
"convert": "auto-convert-images",
"watch": "auto-convert-images-watch",
"resize": "auto-convert-images-resize"
}
}
`
Then run:
- npm run convert - one-time conversion
- npm run watch - watch mode
- npm run resize - resize already converted files
$3
To run the watcher alongside the development server:
`bash
npm install concurrently --save-dev
`
`json
{
"scripts": {
"dev": "concurrently \"npm run watch\" \"next dev\""
}
}
`
š Use Cases
$3
Convert all PNG/JPG files to WebP:
`bash
npx auto-convert-images
`
$3
Automatically convert new images as they're added:
`bash
npx auto-convert-images-watch
`
$3
If you converted files without resize, then decided you need resize:
`bash
npx auto-convert-images-resize
`
- If removeOriginal: false ā creates new files with size suffix: image.webp ā image-1920x1080.webp
- If removeOriginal: true` ā overwrites original files