The method of Node.js batch to add watermark to picture _node.js

Source: Internet
Author: User
Tags add watermark to picture require

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.