# trim-image

> Remove transparent pixels from image borders

Latest version **1.0.3** (published 2017-12-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install trim-image
pnpm add trim-image
yarn add trim-image
bun add trim-image
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2017-12-02 |
| First published | 2017-05-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Renan Vaz |
| Maintainers | renanvaz |
| Keywords | crop, image, png, transparent, trim, trim-image |

## Links

- npm: https://www.npmjs.com/package/trim-image
- Repository: https://github.com/renanvaz/trim-image
- Homepage: https://github.com/renanvaz/trim-image#readme
- Issues: https://github.com/renanvaz/trim-image/issues
- npm.io page: https://npm.io/package/trim-image

## Dependencies (3)

- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [get-pixels](https://npm.io/package/get-pixels.md) ^3.3.0
- [save-pixels](https://npm.io/package/save-pixels.md) ^2.3.4

## Recent versions

- 1.0.3 (latest) — 2017-12-02
- 1.0.2 — 2017-12-02
- 1.0.1 — 2017-05-04
- 1.0.0 — 2017-05-04

## README

# trim-image

> Remove transparent pixels from image borders

## Installation

Install with [npm](https://npmjs.org/package/trim-image):
```
npm install --save trim-image
```

# Params

```
trimImage(filename, filenameOut [, cropInfo|callback [, callback]]);

trimImage(filename, filenameOut);
trimImage(filename, filenameOut, cropInfo);
trimImage(filename, filenameOut, callback);
trimImage(filename, filenameOut, cropInfo, callback);
```

Property           | Necessary | Type         | Plugin default value
-------------------|-----------|--------------|-----------
filename           | yes       | `string`     |
filenameOut        | yes       | `string`     |
cropInfo           | no        | `object`     | `{ top: true, right: true, bottom: true, left: true } `
callback           | no        | `function`   | `(err) => {} `

More detailed explanation is below.

#### filename
The input filename

#### filenameOut
The output filename

#### cropInfo
Defines which sides will be cut. By default, all sides are started as `true`

#### callback
The callback function for async flow and error handle

# Usage
## Simple example
```js
const trimImage   = require('trim-image');
const fs          = require('fs');

trimImage(`images/${filename}`, `out/${filename}`);
```

## Complete example
```js
const trimImage     = require('trim-image');
const fs            = require('fs');
const path          = require('path');

const allFilesSync = (dir, fileList = []) => {
  fs.readdirSync(dir).forEach(file => {
    const filePath = path.join(dir, file)

    fileList.push(
      fs.statSync(filePath).isDirectory()
        ? {[file]: allFilesSync(filePath)}
        : file
    )
  })
  return fileList
}

const images = allFilesSync('images/').filter((filename) => filename.match(/\.png$/));

images.forEach((filename) => {
    trimImage(`images/${filename}`, `out/${filename}`, { top: false, left: false }, (err) => {
        if (err) {
          console.log(err);
          return;
        }
    });
});
```

---
_Source: https://npm.io/package/trim-image · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
