-
Notifications
You must be signed in to change notification settings - Fork 7
/
library.js
59 lines (51 loc) · 1.28 KB
/
library.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
"use strict";
var os = require('os');
var gm = require('gm').subClass({ imageMagick: '7+' });
var fs = require('fs');
const winston = require.main.require('winston');
var plugin = {};
if (os.platform() === 'linux') {
require('child_process').exec('/usr/bin/which convert', function(err, stdout) {
if (err || !stdout) {
winston.warn('Couldn\'t find convert. Did you install imagemagick?');
}
});
}
plugin.resize = function(data, callback) {
var img = gm(data.path);
if (data.path.endsWith('.gif')) {
img = img.coalesce();
}
img.autoOrient()
.resize(data.width || null, data.height || null)
.write(data.target || data.path, function(err) {
callback(err);
});
};
plugin.size = function(data, callback) {
gm(data.path).autoOrient().size(function(err, size) {
if (err) {
return callback(err);
}
var image = {
path: data.path,
width: size.width,
height: size.height
};
callback(null, image);
});
};
plugin.fileTypeAllowed = function(path, callback) {
gm(path).size(function(err) {
callback(err);
});
};
plugin.normalise = function(data, callback) {
gm(data.path).autoOrient().toBuffer('png', function(err, buffer) {
if (err) {
return callback(err);
}
fs.writeFile(data.path + '.png', buffer, 'binary', callback);
});
};
module.exports = plugin;