Node. js watermarks images in batches.

Source: Internet
Author: User
Tags watermark images

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!

Related Article

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.