https://github.com/yinyanfr/image-down
Yet another library / CLI tool to batch compress / downscale images.
https://github.com/yinyanfr/image-down
cli image image-compression image-processing image-resizer reduce-size
Last synced: about 1 year ago
JSON representation
Yet another library / CLI tool to batch compress / downscale images.
- Host: GitHub
- URL: https://github.com/yinyanfr/image-down
- Owner: yinyanfr
- License: mit
- Created: 2023-05-15T15:53:55.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-04T14:35:16.000Z (almost 3 years ago)
- Last Synced: 2025-03-03T17:05:44.554Z (over 1 year ago)
- Topics: cli, image, image-compression, image-processing, image-resizer, reduce-size
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/image-down
- Size: 72.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# image-down
[](https://www.npmjs.com/package/image-down)


[](https://github.com/yinyanfr/image-down/releases/latest)
Yet another CLI tool to batch compress / downscale images.
## :green_book: Quick Start
```bash
npx image-down images/* --width 800 --output compressed
```
```typescript
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',
});
```
## :wrench: Cli
```bash
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
```typescript
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
```typescript
/**
* 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.
```typescript
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`.