节点吉普 | scaleToFit
简介:scaleToFit()函数是 Nodejs 中的内置函数| Jimp 将图像缩放到适合具有给定宽度和高度的矩形内的最大尺寸。
句法:
scaleToFit(w, h, mode, cb)
范围:
- w - 此参数存储图像的宽度。
- h - 此参数存储图像的高度。
- mode – 这是存储缩放方法的可选参数。
- cb – 这是编译完成时调用的可选参数。
输入图像:
设置环境:
npm init -y
安装依赖:
npm install jimp
示例 1:
javascript
// npm install --save jimp
// import jimp library to the environment
var Jimp = require('jimp');
// User-Defined Function to read the images
async function main() {
const image = await Jimp.read('
https://media.geeksforgeeks.org/wp-content/uploads/20190328185307/gfg28.png');
// scaleToFit Function having width and height
image.scaleToFit(300, 300)
.write('scaleToFit1.png');
}
main();
console.log('Image Processing Completed');
javascript
// npm install --save jimp
// import jimp library to the environment
var Jimp = require('jimp');
// User-Defined Function to read the images
async function main() {
const image = await Jimp.read('
https://media.geeksforgeeks.org/wp-content/uploads/20190328185333/gfg111.png');
// scaleToFit Function having width, height, mode and callback function
image.scaleToFit(1000, 1700, Jimp.RESIZE_BEZIER, function(err){
if (err) throw err;
})
.write('scaleToFit2.png');
}
main();
console.log('Image Processing Completed');
输出:
示例 2:使用 mode 和 cb(可选参数)
javascript
// npm install --save jimp
// import jimp library to the environment
var Jimp = require('jimp');
// User-Defined Function to read the images
async function main() {
const image = await Jimp.read('
https://media.geeksforgeeks.org/wp-content/uploads/20190328185333/gfg111.png');
// scaleToFit Function having width, height, mode and callback function
image.scaleToFit(1000, 1700, Jimp.RESIZE_BEZIER, function(err){
if (err) throw err;
})
.write('scaleToFit2.png');
}
main();
console.log('Image Processing Completed');
输出:
参考: https://www.npmjs.com/package/jimp