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,
a plug-in for nodejs image processing tools - gm, which encapsulates GraphicsMagick (GM) and ImageMagick (IM), which is called using spawn.
UseGraphicsMagick (GM) or ImageMagick (IM) are two commonly used image processing tools with basically the same functions. GM is a branch of IM.
the pre-installation software
of nodejs image processing tool gm
toinstall 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. Currently, it is official The provided version is 7.1.x), the 7.0.x download address is http://m.downcc.com/d/398765.
During installation, you must 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 add watermarks, so the following method uses gm to add watermarks.
Load the gm module
const gm = require('gm').subClass({imageMagick: true})
Specify the image to add text
gm(./uploads/pic/test.jpg) //Specify the image to add watermark.stroke("white ") //Outer font color.fill("white") //Inner font color (default is black if not set) .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) //The folder where the font is located and the font size
gm(./uploads/pic/test.jpg) //Specify the picture to add the watermark.stroke(" white") //Outer font color.fill("white") //Inner font color (default is black if not set) .font("./ttf/msyh.ttf",60) //The folder where the font library is located and the font size.drawText(50,50,"中文China") .write(./uploads/pic/watermark.jpg, function (err) { console.log(err) if (!err) console.log('ok'); else console.log(err); });
Add date watermark
to download moment module
npm install moment
load module
const moment = require('moment');
call
var datetime = moment().format("YYYY-MM-DD HH:mm:ss"); gm(./uploads/pic/test.jpg) //Specify the picture to add watermark.stroke("white") //Outer font color.fill("white") //Inner font color (default is black if not set) ) .font("./ttf/msyh.ttf",60) //The folder where the font library is located and the font size.drawText(50,50,datetime) .write(./uploads/pic/watermark.jpg, function (err) { console.log(err) if (!err) console.log('ok'); else console.log(err); });