Remove transparent pixels from image borders
Install with npm:
npm install --save trim-image
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.
The input filename
The output filename
Defines which sides will be cut. By default, all sides are started as true
The callback function for async flow and error handle
const trimImage = require('trim-image');
trimImage(`images/${filename}`, `out/${filename}`);
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;
}
});
});