Skip to content

Instantly share code, notes, and snippets.

@ahmeti
Created November 2, 2018 23:05
Show Gist options
  • Save ahmeti/58115f1ad2d0e9ebac0e8ea8aa8ce277 to your computer and use it in GitHub Desktop.
Save ahmeti/58115f1ad2d0e9ebac0e8ea8aa8ce277 to your computer and use it in GitHub Desktop.
Remove Borders (1px) on Image with NodeJS
var fs = require('fs')
, gm = require('gm').subClass({imageMagick: true});
const imagesFolder = 'public/images/1/featured';
function getFilePaths() {
return new Promise(resolve => {
fs.readdir(imagesFolder, (err, files) => {
resolve(files);
});
});
}
async function run() {
const files = await getFilePaths();
for (const file of files) {
const contents = await imageEdit(imagesFolder + '/' + file);
console.log(contents);
}
}
function imageEdit(file)
{
return new Promise(resolve => {
gm(file)
.size(function (err, size) {
if (!err) {
gm(file)
.resize(size.width + 4, size.height + 4, '^')
.gravity('Center')
.crop(size.width + 2, size.height + 2)
.noProfile()
.write(file, function (err) {
if (!err){
resolve(file)
}
});
}
});
});
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment