forked from flovilmart/parse-image
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
144 lines (120 loc) · 3.33 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
140
141
142
143
var gm = require('gm').subClass({imageMagick: true});
module.exports = Image = function(){}
Image.prototype.setData = function(data, options){
return new Promise((resolve, reject) => {
_setData(this, data, resolve, reject, 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){
var self = this;
var w = options.width;
var h = options.height;
var l = options.left || 0;
var t = options.top || 0;
var r = options.right || 0;
var b = options.bottom || 0;
if (!options.width) {
w = self.width()-r-l;
}
if (!options.height) {
h = self.height()-b-t;
}
var cropped = self._image.crop(w,h,l,t);
return _wrap(self, cropped, options);
}
Image.prototype.quality = function(quality, options) {
var self = this;
return _wrap(self, self._image.quality(quality), options);
}
Image.prototype.scale = function(options){
var 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.resize = function(options){
var self = this;
if (options.ignoreAspectRatio) {
return _wrap(self, self._image.resizeExact(options.width, options.height),options);
} else {
return _wrap(self, self._image.resize(options.width, options.height),options);
}
}
Image.prototype.setFormat = function(format,options){
var self = this;
self._image.setFormat(format.toLowerCase());
return _wrap(self, self._image, options);
}
Image.prototype.format = function(options){
return new Promise((resolve, reject) => {
this._image.format(callbackify(resolve, reject, options));
});
}
Image.prototype.pad = function(options) {
var self = this;
var w = options.width;
var h = options.height;
var l = options.left || 0;
var t = options.top || 0;
var r = options.right || 0;
var b = options.bottom || 0;
if (!options.width) {
w = self.width()+r+l;
}
if (!options.height) {
h = self.height()+b+t;
}
var padded = self._image.out("-background", options.color)
.borderColor(options.color)
.border(l, r)
.extent(w, h)
.out("-flatten")
return _wrap(self, padded, options);
}
var _setData = function(self, data, resolve, reject, options) {
self._data = data;
self._image = gm(data);
self._image.size({bufferStream: true}, function(err, size){
self._size = size;
callbackify(resolve, reject, options)(err, self);
});
}
var _callback = function(self, resolve, reject, options){
options = options || {};
return function(err, buf){
if (err) {
options.error && options.error(err);
reject(err);
} else{
_setData(self, buf, resolve, reject, options);
}
}
}
var _wrap = function(self, gm, options){
return new Promise((resolve, reject) => {
gm.toBuffer(_callback(self, resolve, reject, options));
});
}
var callbackify = function(resolve, reject, options) {
options = options || {};
return function(err, res) {
if (err) {
options.error && options.error(err);
return reject(err);
} else {
options.success && options.success(err);
return resolve(res);
}
};
}