Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use images+imageinfo library to add watermarks to pictures in batch in Node project

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use images+imageinfo library in Node project to add watermarks to pictures in batches. This article is very detailed and has certain reference value. Interested friends must finish reading it!

Nodejs adds watermarks to pictures in batches.

Environmental preparation

Install the images library

Npm install images

Install the imageinfo library

Npm install imageinfo

Realize

This example addimageinfo.js is in the same directory as marklogo.png, imagest folder, and node_modules folder.

It can also be implemented with subfiles

JS code

If you use it directly, you need to modify the information.

Var logomarkimg = images ('. / marklogo.png'); / / watermark location var rmimgpath = ". / imagest/img/"; / / add image file plus location var mark = "logo_"; / / Save image prefix, if "", replace the original image

Complete code addimageinfo.js

/ / reference file system module var fs = require ("fs"); / / reference imageinfo module var imageInfo = require ("imageinfo"); / / reference images module var images = require ('images'); var logomarkimg = images ('. / marklogo.png'); / / watermark location var rmimgpath = ". / imagest/img/"; / / add picture file plus location var mark = "logo_" / / Save the picture prefix. If "", replace the original picture function readFileList (path, filesList) {var files = fs.readdirSync (path); files.forEach (function (itm, index) {var stat = fs.statSync (path + itm) If (stat.isDirectory ()) {/ / Recursive read file readFileList (path + itm + "/", filesList)} else {var obj = {}; / / define the path and name of an object to store the file obj.path = path;// path obj.filename = itm// name filesList.push (obj) }})} var getFiles = {/ / get all the files under the folder getFileList: function (path) {var filesList = []; readFileList (path, filesList); return filesList;}, / / get all the pictures under 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))}); return imageList;}}; / / get all the pictures in the folder var photos = getFiles.getImageFiles (rmimgpath); for (var I = 0; I < photos.length) Var sourceImgpath +) {photos [I] .path; var sourceImgname = photos.filename; var sourceImg = images (sourceImgpath + sourceImgname); var sWidth = sourceImg.width (); var sHeight = sourceImg.height (); var wmWidth = logomarkimg.width (); var wmHeight = logomarkimg.height () Images (sourceImg) / / sets the coordinate location of the drawing, and the distance from the lower right corner is 10px. Draw (logomarkimg, sWidth-wmWidth-10, sHeight-wmHeight-10) / / the save format is automatically recognized. Save (sourceImgpath + mark + sourceImgname +');}

Running

In the directory, run the node command

Node. / addimageinfo.js

View the effect

The above is all the content of the article "how to use images+imageinfo library to add watermarks to pictures in batch in the Node project". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report