Open In App

Node.js GM write() Function

Last Updated : 29 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The write() function is an inbuilt function in the GraphicsMagick library which is used to write an image to a file. If a file already exists, then it will be overwritten. The function returns the true value of success.

Syntax: 

write( filename )

Parameters: This function accepts single parameters as mentioned above and described below:  

  • filename: This parameter is used to specify the name of the file.

Return Value: This function returns the GraphicsMagick object.

Example 1:  

javascript




// Include gm library
const 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
.drawRectangle(10, 100, 260, 210)
 
// 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
.drawRectangle(260, 100, 350, 210)
 
// Invoke drawText in order to
// name the box as 2
.drawText(300, 170, "2")
 
// Call to drawText Function
.drawText(100, 280, "GeeksforGeeks!")
 
// Process and write the image
.write("drawRectangle1.png", function (err) {
    if (!err) console.log('done');
});


Output: 

Example 2: Overwriting the file.  

javascript




// Include gm library
const 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!")
 
// Process and write the image
.write("drawRectangle1.png", function (err) {
    if (!err) console.log('done');
});


Output: 

Reference: https://www.npmjs.com/package/gm



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads