node.js - Electron App中的Imagemin不压缩图像

标签 node.js electron imagemin

我正在尝试使用此代码在 Electron 应用程序中压缩单个 PNG 图像

  const files = await imagemin([filePath], {
    destination: destinationPath,
    plugins: [
      imageminPngquant({
        quality: [0.2, 0.4],
        speed: 1
      })
    ]
  });
  debuglog(files);
filePath 包含 PNG 文件的完整路径,例如
C:\Users\name\Downloads\images\needle.png
该文件确实存在,并且路径是正确的:当我将相同的路径放入 Windows 资源管理器时,png 打开。
destinationPath 包含指向 .png 文件所在目录的路径(换句话说,我想覆盖原始文件),例如
C:\Users\name\Downloads\images
当我运行它时,原始文件保持不变,函数调用返回的变量“files”包含一个空数组。
我究竟做错了什么?有没有办法让调试输出告诉我 imagemin 到底在做什么?
更新 : 这是一个具体的例子。代码如下所示:
  console.log("compressPNG");
  console.log(filePath);
  console.log(path);
  var files = await imagemin([filePath], {
    destination: path,
    plugins: [
      imageminPngquant({
        quality: [0.2, 0.4],
        speed: 1
      })
    ]
  });
  console.log(files);
这会产生以下日志输出:
Log output from running the image compression code

最佳答案

This bug report表示您需要将反斜杠 ( \ ) 转换为正斜杠 ( / )。
根据其中一位评论者的说法,包裹 globbyimagemin依赖于期望带有正斜杠 ( / ) 作为分隔符的文件路径。
这是一个完整的例子:

const imagemin = require("imagemin");
const imageminPngquant = require("imagemin-pngquant");

let input_path = "C:\\path\\to\\file.png";
let output_dir = "C:\\output\\directory";

// Replace backward slashes with forward slashes      <-- Option A
input_path = input_path.replace(/\\/g, "/");
output_dir = output_dir.replace(/\\/g, "/");

(async () => {
  var files = await imagemin([input_path], {
    destination: output_dir,
    // glob: false,                                   <-- Option B
    plugins: [
      imageminPngquant({
        quality: [0.2, 0.4],
        speed: 1
      })
    ]
  });
  console.log(files);
})();
或者,设置 glob: false也应该有助于接受 Windows 文件路径,因为它绕过了 globby 的使用。模块。

关于node.js - Electron App中的Imagemin不压缩图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63858653/

相关文章:

node.js - 如何让 Nightwatch.js 在 Internet Explorer 上运行测试

javascript - 将 Electron 应用程序转换为 UWP 应用程序

c++ - 绑定(bind).gyp : How to use "copies" section to copy files in multiple location

webpack - WebPack 条目是否必须是 JavaScript 文件

node.js - 错误 :0909006C:PEM routines:get_name:no start line - for google cloud platform in heroku

node.js - 类型 'virtual' 上不存在属性 'typeof Schema'

从 node.js shell 和文件运行时,Javascript 代码输出不同

javascript - Electron 将创建者窗口 ID 传递给新的 BrowserWindow

gulp - 如何使用 gulp-imagemin + 插件获得最佳的 PNG 压缩?

node.js - 在使用imagemin通过 Electron 应用程序压缩图像时,如何修复文件错误?