使用 Node 将图像转换为 webp
Convert Images to webp with Node
我有一个将图像转换为节点的脚本,但我遇到了一个问题,即我收到了成功的消息,但没有任何输出。
import imagemin from "imagemin";
import webp from "imagemin-webp";
var outputFolder = "./FSS-assets/webp"; // Output folder
var PNGImages = "./FSS-assets/*.png"; // PNG images
var JPEGImages = "./FSS-assets/*.jpg"; // JPEG images
imagemin([PNGImages], outputFolder, {
plugins: [webp({
lossless: true // Losslessly encode images
})]
}).then(function() {
console.log("Images converted!");
});
imagemin([JPEGImages], outputFolder, {
plugins: [webp({
quality: 65 // Quality setting from 0 to 100
})]
}).then(function() {
console.log("Images converted!");
});
我是运行
- 节点 v14.16.0
- imagemin v8
- imagemin-webp v6
我也尝试过使用相对文件夹路径无济于事。这是在 Windows 10.
那是因为 imagemin
有 2 个参数 input
而 options
你给了 3 个参数。
来自Doc:
imagemin(input, options?)
Returns Promise<object[]> in the format {data: Buffer, sourcePath: string, destinationPath: string}.
因此对于您的代码,它将是:
imagemin([PNGImages], {
destination: outputFolder,
plugins: [webp({
lossless: true // Losslessly encode images
})]
}).then(function() {
console.log("Images converted!");
});
看来您只需要稍微调整一下参数结构即可。成为 ([images], { destination: ‘/output directory’, plugins: {});
const files = await imagemin(['images/*.{jpg,png}'], {
destination: 'build/images',
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
希望这对您有所帮助并奏效。
我有一个将图像转换为节点的脚本,但我遇到了一个问题,即我收到了成功的消息,但没有任何输出。
import imagemin from "imagemin";
import webp from "imagemin-webp";
var outputFolder = "./FSS-assets/webp"; // Output folder
var PNGImages = "./FSS-assets/*.png"; // PNG images
var JPEGImages = "./FSS-assets/*.jpg"; // JPEG images
imagemin([PNGImages], outputFolder, {
plugins: [webp({
lossless: true // Losslessly encode images
})]
}).then(function() {
console.log("Images converted!");
});
imagemin([JPEGImages], outputFolder, {
plugins: [webp({
quality: 65 // Quality setting from 0 to 100
})]
}).then(function() {
console.log("Images converted!");
});
我是运行
- 节点 v14.16.0
- imagemin v8
- imagemin-webp v6
我也尝试过使用相对文件夹路径无济于事。这是在 Windows 10.
那是因为 imagemin
有 2 个参数 input
而 options
你给了 3 个参数。
来自Doc:
imagemin(input, options?)
Returns Promise<object[]> in the format {data: Buffer, sourcePath: string, destinationPath: string}.
因此对于您的代码,它将是:
imagemin([PNGImages], {
destination: outputFolder,
plugins: [webp({
lossless: true // Losslessly encode images
})]
}).then(function() {
console.log("Images converted!");
});
看来您只需要稍微调整一下参数结构即可。成为 ([images], { destination: ‘/output directory’, plugins: {});
const files = await imagemin(['images/*.{jpg,png}'], {
destination: 'build/images',
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
希望这对您有所帮助并奏效。