-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmUtils.js
144 lines (129 loc) · 3.96 KB
/
gmUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var fs = require('fs');
var gm = require('gm');
var request = require('request');
var sharp = require('sharp');
var tmp = require('tmp');
var url = require('url');
var QRCode = require('qrcode');
var Promise = require('bluebird');
var compact = xs => xs.filter(x => !!x);
var isUrlRemote = urlString => {
var parsedUrl = url.parse(urlString);
return parsedUrl && parsedUrl.protocol && !!parsedUrl.protocol.match(/https?:/);
}
var commandifyOptions = options =>
Object.entries(options || {}).reduce((acc, [k, v]) => [...acc, `-${k}`, v], []);
var getTmpFilePath = () =>
new Promise((resolve, reject) => {
tmp.file((tmpError, path, fd, cleanup) => {
if (tmpError) return reject(tmpError);
resolve(path);
});
});
var downloadImageToTmp = url =>
getTmpFilePath().then(
path => new Promise((resolve, reject) => {
var response = request(url);
response.pipe(fs.createWriteStream(path));
response.on('end', () => resolve(path));
response.on('error', reject);
})
);
var downloadImageToStream = url => Promise.resolve(request(url));
var dumpStreamToTmpFile = stream =>
getTmpFilePath().then(
path => new Promise((resolve, reject) => {
if (!stream) return reject(`stream is ${stream}`);
stream.pipe(fs.createWriteStream(path));
stream.on('end', () => resolve(path));
stream.on('error', reject);
})
);
var imageWithOptions = (context = {}) => ({ options, url: imageUrl = '' }) => {
var getImagePath = Promise.resolve(imageUrl);
if (imageUrl.startsWith("$")) {
getImagePath = dumpStreamToTmpFile(context[imageUrl]);
} else if (isUrlRemote(imageUrl)) {
getImagePath = downloadImageToTmp(imageUrl);
}
return getImagePath.then(path => compact([ ...commandifyOptions(options), path ]));
}
/*
{
"imageA": {
"url": "https://...",
"options": { }
},
"imageB": {
"url": "https://...",
"options": { }
},
"options": {
"gravity": "SouthEast"
}
}
*/
var composite = (context = {}) => (params = {}) => new Promise((resolve, reject) => {
Promise.all([
params.imageA, params.imageB, params.mask || {}
].map(imageWithOptions(context))).then(
([ imageA, imageB, mask ]) => {
// gm.composite only supports paths for now
// See https://github.com/aheckmann/gm/blame/c6a6c5a18a65e9b9344955a5cf9d7417db25dff3/README.md#L587
var result = gm().command('composite')
.in(...imageA)
.in(...imageB)
.in(...mask)
.in(...commandifyOptions(params.options));
result.stream((err, outputStream, _) => {
if (err) return reject(err);
resolve(outputStream);
});
},
error => {
console.error(error)
reject(error)
}
);
});
// jpeg-only support
var resize = (context = {}) => (params = {}) =>
downloadImageToStream(params.image).then(imageStream => {
var { width, height } = params;
var parseDim = x => x != null ? parseInt(x) : x;
var transformer = sharp()
.resize(parseDim(width), parseDim(height))
.background('white')
.embed();
return imageStream.pipe(transformer);
});
var drawText = (context = {}) => (params = {}) => {
var options = params.options || {};
return Promise.resolve(
gm(1080, 100, '#FFF').transparent('#FFF')
.fill(options.fill) // srsly? https://stackoverflow.com/a/28701493
.drawText(0, 0, params.text || '', options.gravity || "East")
.font(options.font || 'Helvetica', options.fontSize || 72)
.trim().stream('png')
);
}
var drawQRCode = (context = {}) => (params = {}) =>
getTmpFilePath().then(
path => new Promise((resolve, reject) => {
QRCode.toFile(
path,
params.text || '',
params.options || {},
error => {
if (error) return reject(error);
resolve(fs.createReadStream(path));
}
);
})
);
module.exports = {
composite,
drawQRCode,
drawText,
resize,
};