Nodejs 如何将jpg png转换你为webp文件

js代码

import sharp from 'sharp';
import fs from 'fs';

// Input and output file paths
const inputFilePath = 'input.jpg';
const outputFilePath = 'output.webp';

// Create a readable stream from the input file
const readableStream = fs.createReadStream(inputFilePath);

// Create a writable stream to the output file
const writableStream = fs.createWriteStream(outputFilePath);

// Create a Sharp instance for image processing
const imageProcessing = sharp();

// Configure the image processing operations
imageProcessing.webp();

// Pipe the readable stream through the image processing and then to the writable stream
readableStream.pipe(imageProcessing).pipe(writableStream);

// Handle the completion of the piping process
writableStream.on('finish', () => {
  console.log('Image processing completed!');
});

缺点

  1. 长度和宽度不能超过4096,否则会报错
  2. 转换某些图片的时候会报错,不清楚原因

作者:spike

分类: Nodejs

创作时间:2024-01-30

更新时间:2024-01-30

联系方式放在中括号之中例如[[email protected]],回复评论在开头加上标号例如:#1
Copyright © Your Website 2024
介绍 赞助 Github Rss Sitemap 免责声明 联系