在Node.js中将SVG图像转换为PNG,JPEG,TIFF,WEBP和HEIF格式的方法

2020-06-17 06:40:31易采站长站整理

你应该在项目目录中看到新的JPEG文件。

文档:http://huoche.7234.cn/images/jb51/dqoqmdchdkef()
.toFile(“new-file.tiff”)
.then(function(info) {
console.log(info)
})
.catch(function(err) {
console.log(err)
})

当你运行代码时,应该得到类似的输出:


{
format: 'tiff',
width: 2500,
height: 527,
channels: 3,
premultiplied: false,
size: 65778
}

你应该在项目目录中看到新的TIFF文件。

文档:http://sharp.pixelplumbing.com/en/stable/api-output/#tiff

SVG到WEBP

接下来,将 SVG 文件转换为 WEBP 文件格式。确保你在项目目录的根目录中有一个我们可以使用的SVG文件。

这是完整的代码:


const sharp = require("sharp")

sharp("file.svg")
.webp()
.toFile("new-file.webp")
.then(function(info) {
console.log(info)
})
.catch(function(err) {
console.log(err)
})

输出:


{
format: 'webp',
width: 2500,
height: 527,
channels: 4,
premultiplied: false,
size: 35600
}

你应该在项目目录中看到新的WEBP文件。

文档:http://huoche.7234.cn/images/jb51/q0g412v0cn3()
.toFile(“new-file.heif”)
.then(function(info) {
console.log(info)
})
.catch(function(err) {
console.log(err)
})

你还应该在项目目录中看到新的HEIF文件。

文档:http://sharp.pixelplumbing.com/en/stable/api-output/#png

结论

希望本文能帮助你完成编码工作!也希望大家多多支持软件开发网。

原文:https://coderrocketfuel.com/article/convert-a-svg-image-to-png-jpeg-tiff-webp-and-heif-formats-in-node-js