node.js gm是什么
时间:2022-07-12 18:29
gm是基于node.js的图片处理插件,它封装了图片处理工具GraphicsMagick(GM)和ImageMagick(IM),可使用spawn的方式调用。gm插件不是node默认安装的,需执行“npm install gm -S”进行安装才可使用。 本教程操作环境:windows7系统、nodejs16版,DELL G3电脑。 gm是什么 nodejs图片处理工具的插件--gm,它封装了GraphicsMagick(GM)和ImageMagick(IM),它使用spawn的方式调用。 GraphicsMagick(GM) 或 ImageMagick(IM)是两种常用的图片处理工具,功能基本相同,GM是IM的分支。 nodejs图片处理工具gm的使用 前置软件安装 安装GraphicsMagick或ImageMagick (gm插件支持的IM软件是imagemagickv7.0.X.XX版本,如果下载的IM版本为7.1.x,gm调用不会成功,目前官方提供的版本为7.1.x),7.0.x下载地址http://m.downcc.com/d/398765。 在安装时,安装ImageMagick时一定要要选择画框的部分(gm插件调用的是convert命令) 安装gm 添加水印 使用gm主要还是用来添加水印,因为nodejs本身自带的image模块能满足大部分需求,但是无法添加水印,所以下面就使用gm添加水印的方法。 载入gm模块 指定图片添加文字 添加中文字体 添加日期水印 下载moment模块 加载模块 调用 更多node相关知识,请访问:nodejs 教程! 以上就是node.js gm是什么的详细内容,更多请关注gxlsystem.com其它相关文章!npm install gm -S
const gm = require('gm').subClass({imageMagick: true})
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);
});
.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);
});