【问题标题】:How to convert JPG image to WEBP format in Node.js?如何在 Node.js 中将 JPG 图片转换为 WEBP 格式?
【发布时间】:2018-12-01 21:49:24
【问题描述】:

我正在尝试使用 react.Js 上传图像并使用 multer 中间件将该图像保存在 node.Js 中。这工作得很好,但现在我想使用 webp-converter 将该图像转换为 WEBP 格式,反之亦然。

但我收到此错误:

 Error: Command failed: E:\Zegal\node_modules\webp-converte
 ib/libwebp_win64/bin/cwebp.exe -q 80 2a3279b820cc23939eea0295228423ee-- 
 inspironal-quote-about-life-inspiring-words.jpg -o output.webp
 SHCreateStreamOnFileA(filename, STGM_READ, stream) failed 80070002
 Error opening input file 2a3279b820cc23939eea0295228423ee--inspirational-quo
 about-life-inspiring-words.jpg (80070002)
 OpenInputStream(filename, &stream) failed 80070002
 cannot open input file '2a3279b820cc23939eea0295228423ee--inspirational-quot
 bout-life-inspiring-words.jpg'
 Error! Could not process file 2a3279b820cc23939eea0295228423ee--inspirationa
 uote-about-life-inspiring-words.jpg
 Error! Cannot read input picture file '2a3279b820cc23939eea0295228423ee--ins
 ational-quote-about-life-inspiring-words.jpg'

如何使用 multer 和 WEBP 在节点中解决这个问题?有没有 其他转换解决方案?

const multer = require('multer');
const webp = require('webp-converter');
const path = require('path');
const storage = multer.diskStorage({
    destination: './public/images',
    filename(req, file, cb) {
        cb(null, `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`)
    }
});

const upload = multer({
    storage,
    fileFilter: function (req, file, cb) {
        checkFileType(file, cb)
    }
}).single("image");

checkFileType = (file, cb) => {
    console.log("check file", file);
    const requireMimetype = "image/jpeg";
    const checkMimeType = file.mimetype == requireMimetype ? true : false;
    console.log("checkMimeType", checkMimeType);
    if (checkMimeType) {
        webp.cwebp(file.originalname, "output.webp", "-q 80", function (status) {
            console.log(status);
        });
        return cb(null, true)
    } else {
        cb("Error:Only jpg images are allowed.")
    }
}

module.exports = upload;

【问题讨论】:

标签: node.js express multer webp


【解决方案1】:

我发现这样做很简单。

const sharp = require('sharp');


console.log(req.files.image, "form image is here"); 
console.log(req.files.image.data, "form image buffer data is here");

sharp(req.files.image.data)            
.rotate()            
.resize(200)            
.toBuffer()            
.then( newBuffer => { 

//changing the old jpg image buffer to new webp buffer
 req.files.image.data = newBuffer;

//moving the new webq image to server public folders
 req.files.image.mv(appRoot+'/uploads/images/'+ Date.now() + '.webp', function(err) {                
  if (err) {                    
     return res.status(500).send(err);                
  }
   res.status(200).json({
      message : "image uploaded successfully" 
  })
 })            
})            
.catch( err => { console.log(err) });

希望对你有帮助。

【讨论】:

    【解决方案2】:

    我使用了一个非常棒的图像处理库 sharp,该库提供的功能之一是将图像转换为 webp 图像

    import sharp from 'sharp';
    
    const imageProcessing = sharp();
    
    imageProcessing
    .webp()
    .pipe(someWritableStream);

    【讨论】:

    • 嗨,这可以像 Angular 7 这样在客户端使用吗?因为我看到你正在使用import 语句来调用文件
    • 不幸的是,我认为它根本行不通,因为 sharp 是用 C 和 C++(以及其他非 JS 语言)编写的库的 NodeJS 包装器
    猜你喜欢
    • 1970-01-01
    • 2018-05-10
    • 2019-06-05
    • 2018-09-10
    • 2020-10-02
    • 2015-09-16
    • 2018-12-17
    • 2014-12-06
    • 2018-08-26
    相关资源
    最近更新 更多