nodejs遍历文件夹下并操作HTML/CSS/JS/PNG/JPG的方法

2020-06-17 05:35:24易采站长站整理

var root = path.join(__dirname)+relativePath
// 放大图片并制作副本
function magnifyImg(_path){
// 获取当前图片的长和宽
// 将长和宽放大1.5倍
// 设置新的图片大小
// 没有直接调用magnify,因为magnify不支持小数
gm(_path)
.size(function (err , size){
if (!err){
// console.log(size.width > size.height ? 'wider' : 'taller than you');
let nowWidth =parseInt(size.width) ;
let nowHeight = parseInt(size.height) ;
let magnifyWidth = Math.floor( nowWidth * 1.5);
let magnifyHeight = Math.floor( nowHeight * 1.5);
gm(_path).resizeExact(magnifyWidth,magnifyHeight).write(_path,function(err){
if (!err)
console.log(_path+'done');
else
console.log(_path+'fail'+err);
})

}
else{
console.log('get size has error '+_path + ' and err is : '+err);
}
})
}

至此,功能就完成了。

比较有感触的是在操作代码中相关位置以及大小的实现过程,花了一些心思。图片的操作就是百度之后根据别人写的来做的。