This repository has been archived by the owner on Jan 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
139 lines (116 loc) · 2.94 KB
/
index.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
var gm = require('gm').subClass({ imageMagick: true });
// Require parse if undefined for the promise
if (typeof Parse == 'undefined') {
Parse = require('parse').Parse;
}
module.exports = Image = function() {}
Image.prototype.setData = function(data, options) {
return _setData(this, data, options);
}
Image.prototype.width = function() {
return this._size.width;
}
Image.prototype.height = function() {
return this._size.height;
}
Image.prototype.data = function() {
return Promise.resolve(this._data);
}
Image.prototype.crop = function(options) {
const self = this;
let w = options.width;
let h = options.height;
const l = options.left || 0;
const t = options.top || 0;
const r = options.right || 0;
const b = options.bottom || 0;
if (!options.width) {
w = self.width() - r - l;
}
if (!options.height) {
h = self.height() - b - t;
}
const cropped = self._image.crop(w, h, l, t);
return _wrap(self, cropped, options);
}
Image.prototype.quality = function(quality, options) {
const self = this;
return _wrap(self, self._image.quality(quality), options);
}
Image.prototype.scale = function(options) {
const self = this;
if (options.ratio) {
options.width = options.ratio * self.width();
options.height = options.ratio * self.height();
}
return _wrap(self, self._image.scale(options.width, options.height), options);
}
Image.prototype.setFormat = function(format, options) {
const self = this;
self._image.setFormat(format.toLowerCase());
return _wrap(self, self._image, options);
}
Image.prototype.format = function(options) {
const self = this;
return new Promise(
(resolve, reject) => {
self._image.format((err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
}
);
}
Image.prototype.pad = function(options) {
const self = this;
let w = options.width;
let h = options.height;
const l = options.left || 0;
const t = options.top || 0;
const r = options.right || 0;
const b = options.bottom || 0;
if (!options.width) {
w = self.width() + r + l;
}
if (!options.height) {
h = self.height() + b + t;
}
const padded = self._image.out('-background', options.color)
.borderColor(options.color)
.border(l, r)
.extent(w, h)
.out('-flatten');
return _wrap(self, padded, options);
}
var _wrap = (self, gm, options) => {
return new Promise(
(resolve, reject) => {
gm.toBuffer((err, buf) => {
if (err) {
reject(err);
} else {
resolve(_setData(self, buf, options));
}
});
}
);
}
var _setData = (self, data, options) => {
return new Promise(
(resolve, reject) => {
self._data = data;
self._image = gm(data);
self._image.size({ bufferStream: true }, (err, size) => {
self._size = size;
if (err) {
reject(err);
} else {
resolve(self);
}
});
}
);
}