码迷,mamicode.com
首页 > 其他好文 > 详细

批量压缩图片

时间:2018-10-27 14:54:38      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:ges   批量   dex   rsync   ima   return   res   await   else   

var images = require("images");
var fs = require("fs");
var path = "./images";
async function deleteall (path) {
var files = [];
if(fs.existsSync(path)) {
files = fs.readdirSync(path);
files.forEach(function(file, index) {
var curPath = path + "/" + file;
if(fs.statSync(curPath).isDirectory()) { // recurse
deleteall(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
 
function explorer(path){
fs.readdir(path, async function(err, files){
 
if(err){
console.log(‘error:\n‘ + err);
return;
}
await deleteall(‘./compressImages‘)
fs.mkdir("./compressImages/",function(err){
if (err) {
return console.error(err);
}
files.forEach(function(file){

fs.stat(path + ‘/‘ + file, function(err, stat){
if(err){console.log(err); return;}
if(stat.isDirectory()){
 
explorer(path + ‘/‘ + file);
}else{
 

let name = path + ‘/‘ + file;
let outName = ‘./compressImages‘+ ‘/‘ +file
let width = images(name).width()
if(images(name).width()>750){
images(name)
.size(750)
.save(outName, {
quality : 100
});
} else{
images(name)
 
.save(outName, {
quality : 40
});
}

}
});
 
});
});
 

});
}
explorer(path);

  

批量压缩图片

标签:ges   批量   dex   rsync   ima   return   res   await   else   

原文地址:https://www.cnblogs.com/jinly/p/9860950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!