美文网首页
Node.js 使用 TinifyPng 批量压缩图片

Node.js 使用 TinifyPng 批量压缩图片

作者: 贼噶人 | 来源:发表于2020-05-26 18:35 被阅读0次
const tinify = require('tinify');
const fs = require('fs');
const path = require('path');
tinify.key = 'VzjzXnvLyZncGtY4HzQFKlXst26mP68G';
let imgsPath = path.join(__dirname,'imgs');
let tinifyImgsPath = path.join(__dirname,'tinifyImgs');
if (!fs.existsSync(tinifyImgsPath)) {
    fs.mkdirSync(tinifyImgsPath);
}
fs.readdir(imgsPath,(err,files)=>{
    if (!err) {
        files.forEach((fileName,index)=>{
            tinify.fromFile(path.join(imgsPath,fileName))
            .toFile(path.join(tinifyImgsPath,fileName))
            .then(()=>{
                console.log('tinify ok:%s',fileName);
            })
            .catch((err)=>{
                console.log('tinify err:%s',err);
            });
        })
    }
});

相关文章

网友评论

      本文标题:Node.js 使用 TinifyPng 批量压缩图片

      本文链接:https://www.haomeiwen.com/subject/drwvahtx.html