📜  Node.js GM thumbnail()函数

📅  最后修改于: 2022-05-13 01:56:37.670000             🧑  作者: Mango

Node.js GM thumbnail()函数

thumbnail()函数是 GraphicsMagick 库中的一个内置函数,用于制作给定图像的缩略图。该函数在成功时返回真值。
句法:

thumbnail( x, y )

参数:该函数接受上面提到的两个参数,如下所述:

  • x:此参数用于指定缩略图的宽度。
  • y:此参数用于指定缩略图的高度。

返回值:此函数返回 GraphicsMagick 对象。
原图:

示例 1:

javascript
// Include gm library
var gm = require('gm');
  
// Import the image
gm('1.png')
  
// Invoke thumbnail function x, y as 300, 200
.thumbnail(300, 200)
  
// Process and Write the image
.write("thumbnail1.png", function (err) {
  if (!err) console.log('done');
});


javascript
// Include gm library
var gm = require('gm');
  
// Import the image
gm(600, 300, 'white')
  
// set the color for the stroke
.stroke("green", 3)
  
// Set the font 
.font("Helvetica.ttf", 60)
  
//Call to drawText Function
.drawText(100, 280, "GeeksforGeeks!")
  
// Invoke thumbnail function
//  on x, y as 300, 150
.thumbnail(300, 150)
  
// Process and write the image 
.write("thumbnail2.png", function (err) {
  if (!err) console.log('done');
});


输出:

示例 2:

javascript

// Include gm library
var gm = require('gm');
  
// Import the image
gm(600, 300, 'white')
  
// set the color for the stroke
.stroke("green", 3)
  
// Set the font 
.font("Helvetica.ttf", 60)
  
//Call to drawText Function
.drawText(100, 280, "GeeksforGeeks!")
  
// Invoke thumbnail function
//  on x, y as 300, 150
.thumbnail(300, 150)
  
// Process and write the image 
.write("thumbnail2.png", function (err) {
  if (!err) console.log('done');
});

输出:

参考:

  • http://www.graphicsmagick.org/GraphicsMagick.html#details-rotate
  • https://www.npmjs.com/package/gm