imageTemplateGenerator
![NPM version](https://img.shields.io/npm/v/image-template-generator.svg)
通过创建的模板,快速生成对应的图片,用以:自动生成营销分享图片、批量生成不同二维码的图片等等
![](http://wx4.sinaimg.cn/large/7171171cgy1fr2ks4nozjg20mz0buu0x.gif)
一、入门
1、环境依赖
imageTemplateGenerator 基于 gm ,所以同gm的依赖,需要安装对应的工具。
参考: https://github.com/aheckmann/gm#getting-started
$ brew install imagemagick
$ brew install graphicsmagick
$ brew install ghostscript
2、试用
下载文件到任意目录,并安装依赖:
# 仓储有一套中文字体,clone会比较慢,请耐心等候
$ git clone git://github.com/xiongwilee/imageTemplateGenerator.git
$ cd imageTemplateGenerator
$ npm install
执行示例文件:
$ cd example
$ node index.js
提示:生成图片成功: ./merged.png !
,则说明操作成功,可以查看生成的example/merged.png
文件。
二、详细说明
1、使用方法
1)安装依赖
说明: imagemagick
, graphicsmagick
, ghostscript
也是必须的,见上文。
$ npm install image-template-generator
2)使用
用法一:
const Itg = require('image-template-generator');
Itg(bgImg, { })
.then((temp)=>{
return temp.gen({}, { })
})
.then((result)=>{
})
用法二:
const Itg = require('image-template-generator');
Itg(bgImg, { }, {}, { })
.then((result)=>{
})
可参考: example/index.js
,此外,bgImg
, template config
, item config
,options
详细配置说明见下文。
2、详细配置
1)生成模板
Itg(bgImg[, Template config])
例如:
const Itg = require('image-template-generator');
const bgImg = '<Buffer|Stream|Url<String>|Path<String>|Request Config<Object>'
Itg(bgImg, { })
.then((temp)=>{
})
bgImg
整体的背景图片,可以是:
Buffer
: 文件BufferStream
: 文件流,例如:请求图片的http responseUrl<String>
: 图片的链接Path<String>
: 图片的文件系统路径Request Config<Object>
: requestjs 的请求配置,参考requestjs的配置文档
template config
模板的配置,用以说明每个元素的大小、位置、默认图片等属性。
{
logo: {
size: '144,74',
position: '+118+20',
default: 'http://img002.qufenqi.com/products/ac/04/ac04decbbd372b5289e1bf1be30fad99.png'
},
title: {
size: '320,60',
position: '+28+380',
style: {
fontSize: '14',
color: '#333333'
},
default: '更多商品,敬请期待'
},
slogan: {
size: '343,56',
position: '+14+430',
default: path.resolve('../images/slogan.png')
},
qrcode: {
size: '91,91',
position: '+93+528',
default: 'http://img003.qufenqi.com/products/cb/9f/cb9fbcf2eddb111b08ec6c0795900060.png'
},
{
}
}
2)通过模板生成图片
temp.gen(itemsConf[, options])
例如:
temp.gen({}, { })
.then((data)=>{
})
itemsConf
生成图片的元素配置,key和template config
的key一致(如果使用默认,不用配置即可),例如:
{
banner: 'https://img002.qufenqi.com/products/e0/af/e0afcc5a1350f4966f963bc0bff6aafa.jpg',
title: 'Apple iPhone X (A1865) 64GB 深空灰色 移动联通\n电信4G手机 ',
logo: {
method: 'GET',
url: 'https://www.baidu.com/img/bd_logo1.png',
encoding: null
},
qrcode: path.resolve('../images/qrcode.png')
}
key对应的值,和bgImg一致,包括:<Buffer|Stream|Url<String>|Path<String>|Request Config<Object>
。
options
type
: String, 可以配置为'Buffer'/'Stream'/'Path'path
: Sting,如果options配置为"Path",则需要配置写入文件系统的路径
以上为详细配置,综合示例可参考:example/index.js
。
3、TODO
三、贡献
欢迎提issue、fork;有任何疑问也可以邮件联系:xiongwilee[at]foxmail.com。