Node. js watermarks images in batches.
I. preparations:
First, read this article: http://www.bkjia.com/article/97391.htm.
Then, we install a module of node. js: imageinfo.
Npm install imageinfo
Ii. Go directly to the DEMO:
The procedure is as follows:
Step 1: folder structure
Step 2: JS Code
// Reference the file system module var fs = require ("fs"); // reference the imageinfo module var imageInfo = require ("imageinfo "); // reference the 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 read the file readFileList (path + itm + "/", filesList)} else {var obj = {}; // define the path and name of an object to store files. obj. path = path; // path obj. filename = itm // name filesList. push (obj) ;}}var getFiles ={// get all the files in the folder getFileList: function (path) {var filesList = []; readFileList (path, filesList); return filesList;}, // get all the images in the 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 ;}; // obtain all images in the 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 sHeight = sourceImg. height (); var wmWidth = watermarkImg. width (); var wmHeight = watermarkImg. height (); images (sourceImg) // you can specify the position of the drawn coordinate. The distance from the bottom right corner is 40px. draw (watermarkImg, sWidth-wmWidth-40, sHeight-wmHeight-40) // The save format is automatically recognized. save ('. /saveImg/'+ sourceImgName + '');}
Step 3: after running the node command, the folder structure is shown in figure
Step 4: View watermark images in batches
The above is a small part of the Node. I hope js will help you to add watermarks to images in batches. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!