About JIMP:
JavaScript Image Manipulation Program
The "JavaScript Image Manipulation Program" :-)
An image processing library for Node written entirely in JavaScript, with zero native dependencies.
The default jimp configuration.
More and example: https://www.npmjs.com/package/jimp
Nodejs resize images same size by Jimp: (batch change photos to the same size)
Need find max size of images (max height and max width) in folder and then resize (creat file "./jimp.js")
//
const Jimp = require("jimp");
module.exports.findMaxSz = async function (arrFile) {
var image, x=0, y=0;
var images= [];
for (var i = 0; i < arrFile.length; i++) {
image = await Jimp.read(arrFile[i].path);
images.push({
jimp: image,
path: arrFile[i].path,
caption: arrFile[i].caption
});
if(!x || x<image.bitmap.width) x= image.bitmap.width;
if(!y || y<image.bitmap.height) y= image.bitmap.height;
}
return {
width: x,
height: y,
images
}
};
module.exports.reSize = async function (x, y, opt, bg, text) {
// bg: 'black'
var image2= opt.jimp;
if(image2.bitmap.width==x && image2.bitmap.height== y) {
if(text) {
var font = await Jimp.loadFont(Jimp.FONT_SANS_32_WHITE);
image2.print(font, 0, 0, {
text,
alignmentX: Jimp.HORIZONTAL_ALIGN_RIGHT,
alignmentY: Jimp.VERTICAL_ALIGN_BOTTOM
}, image2.bitmap.width, image2.bitmap.height);
console.log('texted and saved done!');
return image2.write(opt.path); // save
}
else return '_';
}
var image = await Jimp.read(x, y, bg);
await image.composite(image2, Math.abs(x - image2.bitmap.width) / 2, Math.abs(y - image2.bitmap.height) / 2, [
{
mode: Jimp.BLEND_SCREEN,
opacitySource: 0.1,
opacityDest: 1
}
]);
if(text) {
var font = await Jimp.loadFont(Jimp.FONT_SANS_32_WHITE);
image.print(font, 0, 0, {
text,
alignmentX: Jimp.HORIZONTAL_ALIGN_RIGHT,
alignmentY: Jimp.VERTICAL_ALIGN_BOTTOM
}, image.bitmap.width, image.bitmap.height);
}
console.log('resized, texted and saved done!');
return image.write(opt.path); // save
}
//
Example:
//
var images = [];
const fs = require('fs');
fs.readdirSync('anh').forEach(function (el, idx) {
images.push({
path: __dirname + '\\images\\' + el,
caption: 'by Nodejs Auto'
})
});
const meJimp= require('./jimp');
; (async function () {
var a = await meJimp.findMaxSz(images);
for(var i=0; i< a.images.length; i++){
await meJimp.reSize(a.width, a.height, a.images[i], 'black', a.images[i].caption);
}
})().catch(function(_ex){
console.error(_ex);
});
//
Comments
Post a Comment