First, the preparatory work:
First, you have to read this article: Http://www.jb51.net/article/97391.htm.
Then, we install a node.js module: Imageinfo.
NPM Install Imageinfo
Second, directly on the demo:
The steps are as follows:
Step1: Folder structure
Step2:js Code
Reference file system module var fs = require ("FS");
Reference Imageinfo module var imageinfo = require ("Imageinfo");
Reference Images module var images = require (' images ');
var watermarkimg = images (' water_logo.png '); function readfilelist (path, fileslist) {var files = fs.readdirsync (path); Files.foreach (function (ITM, index) {var stat
= Fs.statsync (path + ITM); if (Stat.isdirectory ()) {//recursively reads the file readfilelist (path + ITM + "/", Fileslist)} else {var obj = {};//defines the path and name obj for an object holding file.
Path = path;//Path obj.filename = itm//name Fileslist.push (obj); var GetFiles = {//Get all Files under folder Getfilelist:function (path) {var fileslist = []; readfilelist (path, fileslist); re
Turn fileslist; },//Get all Pictures under folder Getimagefiles:function (path) {var imageList = []; this.getfilelist (path). ForEach ((item) => {var ms
= Imageinfo (Fs.readfilesync (Item.path + item.filename));
Ms.mimetype && (Imagelist.push (Item.filename))});
return imageList;
}
};
Get all pictures under folder var photos = Getfiles.getimagefiles ("./public/"); for (var i = 0; i < PHotos.length; i++) {var sourceimg = images ('./public/' +photos[i]); var sourceimgname = photos[i]; var swidth = Sourceimg.width (); var s
Height = Sourceimg.height ();
var wmwidth = Watermarkimg.width ();
var wmheight = Watermarkimg.height ();
Images (SOURCEIMG)//Set the coordinates of the drawing, the lower right corner from 40px. Draw (Watermarkimg, swidth-wmwidth-40, sheight-wmheight-40)//Save format will be automatically recognized
. Save ('./saveimg/' + sourceimgname+ '); }
Step3: After running the node command, the folder structure is shown below
Step4: View the bulk watermark picture
The above is a small set to introduce the Node.js batch to the image of the watermark method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!