|
- //获取项目工程里的图片
- var fs = require('fs');//引用文件系统模块
- var image = require("imageinfo"); //引用imageinfo模块
-
- function readFileList(path, filesList) {
- var files = fs.readdirSync(path);
- files.forEach(function (itm, index) {
- var stat = fs.statSync(path + itm);
- if (stat.isDirectory()) {
- //递归读取文件
- readFileList(path + itm + "/", filesList)
- } else {
-
- var obj = {};//定义一个对象存放文件的路径和名字
- obj.path = path;//路径
- obj.filename = itm//名字
- filesList.push(obj);
- }
-
- })
-
- }
- var getFiles = {
- //获取文件夹下的所有文件
- getFileList: function (path) {
- var filesList = [];
- readFileList(path, filesList);
- return filesList;
- },
- //获取文件夹下的所有图片
- getImageFiles: function (path) {
- var imageInfoList = [];
-
- this.getFileList(path).forEach((item) => {
- var ms = image(fs.readFileSync(item.path + item.filename));
- if( ms.mimeType){
- var json ={};
- json.imageName = item.filename;
- json.imageWidth = ms.width;
- json.imageHeight = ms.height;
- json.imageArea = ms.width * ms.height;
- imageInfoList.push(json);
- }
- });
- return imageInfoList;
-
- }
- };
- //获取文件夹下的所有图片
- var imageInfoList = getFiles.getImageFiles("./");
- console.log("所有图片信息:"+JSON.stringify(imageInfoList));
- var totalImageCount = 0;
- var totalImageArea = 0;
- for(var i =0;i<imageInfoList.length;i++){
- totalImageCount++;
- totalImageArea+=imageInfoList[i]["imageArea"];
- }
- console.log("总图片数:"+totalImageCount);
- console.log("总图片面积:"+totalImageArea);
- //获取文件夹下的所有文件
- // getFiles.getFileList("./");
复制代码 要下载https://github.com/NorgannasAddOns/node-imageinfo 库,直接扔到文件夹下运行就好
|
|