1.0.3 • Published 6 years ago

trim-image v1.0.3

Weekly downloads
19
License
MIT
Repository
github
Last release
6 years ago

trim-image

Remove transparent pixels from image borders

Installation

Install with npm:

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);
PropertyNecessaryTypePlugin default value
filenameyesstring
filenameOutyesstring
cropInfonoobject{ top: true, right: true, bottom: true, left: true }
callbacknofunction(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

const trimImage   = require('trim-image');
const fs          = require('fs');

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

Complete example

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;
        }
    });
});