代码片段

  1. /**
  2. * 帮助文档
  3. * -------
  4. *
  5. * 获取帮助
  6. * 指令 -h
  7. *
  8. * 获取命令执行文件夹
  9. * 指令 -f
  10. * 参数 ./
  11. * 必填,待处理的图片文件夹
  12. *
  13. * 获取是否深度递归处理图片文件夹
  14. * 指令 -deep
  15. * 可选,默认不深度递归
  16. *
  17. * 命令行脚本参考示例
  18. * > node ./tinypng.js -f ./test -deep
  19. * */
  20. const fs = require('fs');
  21. const path = require('path');
  22. const https = require('https');
  23. const URL = require('url').URL;
  24. const EventEmitter = require('events');
  25. const err = msg => new EventEmitter().emit('error', msg);
  26. if (getHelp()) return false;
  27. const conf = {
  28. files: [],
  29. EntryFolder: getEntryFolder(),
  30. DeepLoop: getDeepLoop(),
  31. Exts: ['.jpg', '.png'],
  32. Max: 5200000, // 5MB == 5242848.754299136
  33. }
  34. fileFilter(conf.EntryFolder)
  35. console.log("本次执行脚本的配置:", conf);
  36. console.log("等待处理文件的数量:", conf.files.length)
  37. conf.files.forEach(img => fileUpload(img));
  38. //////////////////////////////// 工具函数
  39. /**
  40. * 获取帮助命令
  41. * 指令 -h
  42. */
  43. function getHelp() {
  44. let i = process.argv.findIndex(i => i === "-h");
  45. if (i !== -1) {
  46. console.log(`
  47. * 帮助文档
  48. * -------
  49. *
  50. * 获取帮助
  51. * 指令 -h
  52. *
  53. * 获取命令执行文件夹
  54. * 指令 -f
  55. * 参数 ./
  56. * 必填,待处理的图片文件夹
  57. *
  58. * 获取是否深度递归处理图片文件夹
  59. * 指令 -deep
  60. * 可选,默认不深度递归
  61. *
  62. * > node ./tinypng.js -f ./test -deep
  63. `)
  64. return true;
  65. }
  66. }
  67. /**
  68. * 获取命令执行文件夹
  69. * 指令 -f
  70. * 参数 ./
  71. * 必填,待处理的图片文件夹
  72. */
  73. function getEntryFolder() {
  74. let i = process.argv.findIndex(i => i === "-f");
  75. if (i === -1 || !process.argv[i + 1]) return err('获取命令执行文件夹:失败');
  76. return process.argv[i + 1];
  77. }
  78. /**
  79. * 获取是否深度递归处理图片文件夹
  80. * 指令 -deep
  81. * 可选,默认不深度递归
  82. */
  83. function getDeepLoop() {
  84. return process.argv.findIndex(i => i === "-deep") !== -1;
  85. }
  86. /**
  87. * 过滤待处理文件夹,得到待处理文件列表
  88. * @param {*} folder 待处理文件夹
  89. * @param {*} files 待处理文件列表
  90. */
  91. function fileFilter(folder) {
  92. // 读取文件夹
  93. fs.readdirSync(folder).forEach(file => {
  94. let fullFilePath = path.join(folder, file)
  95. // 读取文件信息
  96. let fileStat = fs.statSync(fullFilePath);
  97. // 过滤文件安全性/大小限制/后缀名
  98. if (fileStat.size <= conf.Max && fileStat.isFile() && conf.Exts.includes(path.extname(file))) conf.files.push(fullFilePath);
  99. // 是都要深度递归处理文件夹
  100. else if (conf.DeepLoop && fileStat.isDirectory()) fileFilter(fullFilePath);
  101. });
  102. }
  103. /**
  104. * TinyPng 远程压缩 HTTPS 请求的配置生成方法
  105. */
  106. function getAjaxOptions() {
  107. return {
  108. method: 'POST',
  109. hostname: 'tinypng.com',
  110. path: '/web/shrink',
  111. headers: {
  112. rejectUnauthorized: false,
  113. "X-Forwarded-For": Array(4).fill(1).map(() => parseInt(Math.random() * 254 + 1)).join('.'),
  114. 'Postman-Token': Date.now(),
  115. 'Cache-Control': 'no-cache',
  116. 'Content-Type': 'application/x-www-form-urlencoded',
  117. 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
  118. }
  119. }
  120. }
  121. /**
  122. * TinyPng 远程压缩 HTTPS 请求
  123. * @param {string} img 待处理的文件
  124. * @success {
  125. * "input": { "size": 887, "type": "image/png" },
  126. * "output": { "size": 785, "type": "image/png", "width": 81, "height": 81, "ratio": 0.885, "url": "https://tinypng.com/web/output/7aztz90nq5p9545zch8gjzqg5ubdatd6" }
  127. * }
  128. * @error {"error": "Bad request", "message" : "Request is invalid"}
  129. */
  130. function fileUpload(imgPath) {
  131. let req = https.request(getAjaxOptions(), (res) => {
  132. res.on('data', buf => {
  133. let obj = JSON.parse(buf?buf.toString():'{}');
  134. if (obj.error) console.log(`压缩失败!\n 当前文件:${imgPath} \n ${obj.message}`);
  135. else fileUpdate(imgPath, obj);
  136. });
  137. });
  138. req.write(fs.readFileSync(imgPath), 'binary');
  139. req.on('error', e => console.error(`请求错误! \n 当前文件:${imgPath} \n`, e));
  140. req.end();
  141. }
  142. // 该方法被循环调用,请求图片数据
  143. function fileUpdate(entryImgPath, obj) {
  144. let options = new URL(obj.output.url);
  145. let req = https.request(options, res => {
  146. let body = '';
  147. res.setEncoding('binary');
  148. res.on('data', (data) => body += data);
  149. res.on('end', () => {
  150. fs.writeFile(entryImgPath, body, 'binary', err => {
  151. if (err) return console.error(err);
  152. let log = '压缩成功';
  153. log += `优化比例: ${ (( 1 - obj.output.ratio) * 100).toFixed(2) }% ,`;
  154. log += `原始大小: ${ (obj.input.size / 1024).toFixed(2) }KB ,`;
  155. log += `压缩大小: ${ (obj.output.size / 1024).toFixed(2) }KB ,`;
  156. log += `文件:${entryImgPath}`
  157. console.log(log);
  158. });
  159. });
  160. });
  161. req.on('error', e => console.error(e));
  162. req.end();
  163. }
  164. // node ./tinypng.js -f ./static/smp/m/course_task

使用方式

  1. node ./tinypng.js -f ./test -deep

参考文章

  1. nodejs 全自动使用 Tinypng (免费版,无需任何配置)压缩图片