如何在Node js中将JPG图像转换为WEBP格式? [英] How to convert JPG image to WEBP format in Node js ?

查看:813
本文介绍了如何在Node js中将JPG图像转换为WEBP格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   I m trying to upload an image using reactJs and save that image in nodeJs 
   using multer middleware.This is working perfectly but now I want to 
   convert that image to webP format using webp-converter which is a small 
   node.js library for converting other image format to webp format and 
   vice- versa.   
      But I m getting this error :  

错误:命令失败: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(文件名,STGM_READ,流)失败80070002 打开输入文件2a3279b820cc23939eea0295228423ee时出错-鼓舞人心的 about-life-inspiring-words.jpg(80070002) OpenInputStream(文件名和流)失败80070002 无法打开输入文件'2a3279b820cc23939eea0295228423ee--inspirational-quote bout-life-inspiring-words.jpg" 错误!无法处理文件2a3279b820cc23939eea0295228423ee--inspirationa uote-about-life-inspiring-words.jpg 错误!无法读取输入图片文件'2a3279b820cc23939eea0295228423ee--ins 引用quote-about-life-inspiring-words.jpg"

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'

How can I solve this problem in node using multer and webp ? Is there any 
other solution for conversion ?

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");

}).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 =上传;

module.exports = upload;

推荐答案

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

I had used a really awesome image processing library named sharp, and one of the features that that library offer is to convert an image into a webp image

import sharp from 'sharp';

const imageProcessing = sharp();

imageProcessing
.webp()
.pipe(someWritableStream);

这篇关于如何在Node js中将JPG图像转换为WEBP格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆