var gm = require('gm');
var imageMagick = gm.subClass({ imageMagick : true });var path = require('path');var fs= require('fs');var imgDir = path.join(process.cwd()+'/public/img'); var resize = function(file){ imageMagick(file) .resize(750, 450) //加('!')强行把图片缩放成对应尺寸150*150! .autoOrient() .write(file, function(err){ if(err){ return res.end('error|'+err.message); } res.sendFile(file) }); } var readDir = function(fileDir){ fs.readdir(fileDir,function(err,files){ if(err) return console.log(err); files.forEach(function(file){ var filePath = fileDir+'/'+file; fs.stat(filePath,function(err,stats){ if(stats.isDirectory()){ readDir(filePath) }else{ if(/(\.jpg|\.png)$/i.test(filePath)){ resize(filePath) } } }) }) }) } readDir(imgDir);