Home > Web Front-end > JS Tutorial > What is node.js gm

What is node.js gm

青灯夜游
Release: 2022-07-12 18:29:22
Original
3849 people have browsed it

gm is an image processing plug-in based on node.js. It encapsulates the image processing tools GraphicsMagick (GM) and ImageMagick (IM) and can be called using spawn. The gm plug-in is not installed by default in node. You need to execute "npm install gm -S" to install it before it can be used.

What is node.js gm

The operating environment of this tutorial: Windows 7 system, nodejs version 16, DELL G3 computer.

What is gm

nodejs image processing tool plug-in-gm, which encapsulates GraphicsMagick (GM) and ImageMagick (IM), It is called using spawn.

GraphicsMagick (GM) or ImageMagick (IM) are two commonly used image processing tools with basically the same functions. GM is a branch of IM.

Usage of nodejs image processing tool gm

Pre-installation software

Install GraphicsMagick or ImageMagick

(The IM software supported by the gm plug-in is imagemagickv7.0.X.XX version. If the downloaded IM version is 7.1.x, the gm call will not succeed. The currently officially provided version is 7.1 .x), 7.0.x download address http://m.downcc.com/d/398765.

When installing, be sure to select the part of the picture frame when installing ImageMagick (the gm plug-in calls the convert command)

What is node.js gm

Install gm

npm install gm -S
Copy after login

Add watermark

Using gm is mainly used to add watermarks, because the image module that comes with nodejs can meet most needs, but it cannot be added. Watermark, so let’s use the gm method to add a watermark.

  • Load gm module

const gm = require('gm').subClass({imageMagick: true})
Copy after login
  • Specify the picture to add text

gm(./uploads/pic/test.jpg)	//指定添加水印的图片
	.stroke("white")		//字体外围颜色
    .fill("white")			//字体内围颜色(不设置默认为黑色)
    .drawText(50,50,"China")
    .write(./uploads/pic/watermark.jpg, function (err) {
            console.log(err)
            if (!err) console.log('ok');
            else console.log(err);
        });
Copy after login

What is node.js gm

  • Add Chinese font

.font("./ttf/msyh.ttf",60) //字库所在文件夹和字体大小
Copy after login
gm(./uploads/pic/test.jpg)	//指定添加水印的图片
	.stroke("white")		//字体外围颜色
    .fill("white")			//字体内围颜色(不设置默认为黑色)
    .font("./ttf/msyh.ttf",60) //字库所在文件夹和字体大小
    .drawText(50,50,"中文China")
    .write(./uploads/pic/watermark.jpg, function (err) {
            console.log(err)
            if (!err) console.log('ok');
            else console.log(err);
        });
Copy after login

What is node.js gm

  • ##Add date Watermark

Download moment module

npm install moment
Copy after login

Load module

const moment = require('moment');
Copy after login

Calling

var datetime = moment().format("YYYY-MM-DD HH:mm:ss");
gm(./uploads/pic/test.jpg)	//指定添加水印的图片
	.stroke("white")		//字体外围颜色
    .fill("white")			//字体内围颜色(不设置默认为黑色)
    .font("./ttf/msyh.ttf",60) //字库所在文件夹和字体大小
    .drawText(50,50,datetime)
    .write(./uploads/pic/watermark.jpg, function (err) {
            console.log(err)
            if (!err) console.log('ok');
            else console.log(err);
        });
Copy after login

What is node.js gm

Update For more node-related knowledge, please visit:

nodejs tutorial!

The above is the detailed content of What is node.js gm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template