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.
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)
Install gm
npm install gm -S
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})
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); });
Add Chinese font
.font("./ttf/msyh.ttf",60) //字库所在文件夹和字体大小
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); });
npm install moment
const moment = require('moment');
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); });
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!