📜  Node.js GM transparent()函数

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

Node.js GM transparent()函数

transparent()函数是 GraphicsMagick 库中的一个内置函数,用于使指定颜色在图像中透明。该函数在成功时返回真值。

句法:

transparent( color )

参数:此函数接受如上所述和如下所述的单个参数:

  • color:此参数用于指定颜色的名称。

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

原图:

示例 1:

Javascript
// Include gm library
var gm = require('gm');
  
// Import the image
gm('1.png')
  
// Invoke transparent function on white color
.transparent('white')
  
// Process and Write the image
.write("transparent1.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)
  
// Invoke drawRectangle function with x0: 10,
// y0: 100, x1:260, y1:210, wc: 40, wh: 40
.drawRectangle(10, 100, 260, 210, 40, 40)
  
// Invoke drawText in order to name the box as 1 
.drawText(150, 170, "1")
  
// Invoke drawrectangle function with x0: 260,
// y0:100, x1: 350, y1: 210, wc: 80, wh: 80
.drawRectangle(260, 100, 350, 210, 80, 80)
  
// Invoke drawText in order to name the box as 2
.drawText(300, 170, "2")
  
// Call to drawText Function
.drawText(100, 280, "GeeksforGeeks!")
  
// Invoke transparent function on green color
.transparent('green')
  
// Process and write the image 
.write("transparent2.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)
  
// Invoke drawRectangle function with x0: 10,
// y0: 100, x1:260, y1:210, wc: 40, wh: 40
.drawRectangle(10, 100, 260, 210, 40, 40)
  
// Invoke drawText in order to name the box as 1 
.drawText(150, 170, "1")
  
// Invoke drawrectangle function with x0: 260,
// y0:100, x1: 350, y1: 210, wc: 80, wh: 80
.drawRectangle(260, 100, 350, 210, 80, 80)
  
// Invoke drawText in order to name the box as 2
.drawText(300, 170, "2")
  
// Call to drawText Function
.drawText(100, 280, "GeeksforGeeks!")
  
// Invoke transparent function on green color
.transparent('green')
  
// Process and write the image 
.write("transparent2.png", function (err) {
  if (!err) console.log('done');
});

输出:

参考:

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