jimp 0.5.2 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -13
- package/browser/lib/jimp.js +175 -13
- package/browser/lib/jimp.min.js +2 -2
- package/jimp.d.ts +12 -8
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -85,38 +85,60 @@ The static `Jimp.read` method takes the path to a file, URL, dimensions, a Jimp
|
|
|
85
85
|
```js
|
|
86
86
|
Jimp.read('./path/to/image.jpg')
|
|
87
87
|
.then(image => {
|
|
88
|
-
//
|
|
88
|
+
// Do stuff with the image.
|
|
89
89
|
})
|
|
90
90
|
.catch(err => {
|
|
91
|
-
//
|
|
91
|
+
// Handle an exception.
|
|
92
92
|
});
|
|
93
|
-
|
|
94
|
-
Jimp.read(lenna.
|
|
93
|
+
|
|
94
|
+
Jimp.read('http://www.example.com/path/to/lenna.jpg')
|
|
95
95
|
.then(image => {
|
|
96
|
-
//
|
|
96
|
+
// Do stuff with the image.
|
|
97
97
|
})
|
|
98
98
|
.catch(err => {
|
|
99
|
-
//
|
|
99
|
+
// Handle an exception.
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
Jimp.read(jimpInstance)
|
|
103
|
+
.then(image => {
|
|
104
|
+
// Do stuff with the image.
|
|
105
|
+
})
|
|
106
|
+
.catch(err => {
|
|
107
|
+
// Handle an exception.
|
|
100
108
|
});
|
|
101
109
|
|
|
102
|
-
Jimp.read(
|
|
110
|
+
Jimp.read(buffer)
|
|
103
111
|
.then(image => {
|
|
104
|
-
//
|
|
112
|
+
// Do stuff with the image.
|
|
105
113
|
})
|
|
106
114
|
.catch(err => {
|
|
107
|
-
//
|
|
115
|
+
// Handle an exception.
|
|
108
116
|
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
In some cases, you need to pass additional parameters with an image's URL. You can pass an object to the `Jimp.read` method:
|
|
109
120
|
|
|
110
|
-
|
|
121
|
+
```js
|
|
122
|
+
Jimp.read({
|
|
123
|
+
url: 'http://www.example.com/path/to/lenna.jpg', // Required!
|
|
124
|
+
headers: {},
|
|
125
|
+
...
|
|
126
|
+
})
|
|
111
127
|
.then(image => {
|
|
112
|
-
//
|
|
128
|
+
// Do stuff with the image.
|
|
113
129
|
})
|
|
114
130
|
.catch(err => {
|
|
115
|
-
//
|
|
131
|
+
// Handle an exception.
|
|
116
132
|
});
|
|
117
133
|
```
|
|
118
134
|
|
|
119
|
-
|
|
135
|
+
Jimp uses [phin](https://github.com/ethanent/phin) as it's HTTP client. Phin uses [`http.request(options[, callback])`](https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_http_request_options_callback) or [`https.request(options[, callback])`](https://nodejs.org/dist/latest-v8.x/docs/api/https.html#https_https_request_options_callback) methods for making HTTP requests. Phin parses a `url` with the `url.parse(...)` method and passes it with all the other parameters as an `options` to the [`http.request(options[, callback])`](https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_http_request_options_callback) or [`https.request(options[, callback])`](https://nodejs.org/dist/latest-v8.x/docs/api/https.html#https_https_request_options_callback) methods.
|
|
136
|
+
|
|
137
|
+
Briefly speaking, you can pass any options from [`http.request(options[, callback])`](https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_http_request_options_callback), [`https.request(options[, callback])`](https://nodejs.org/dist/latest-v8.x/docs/api/https.html#https_https_request_options_callback) or even [`tls.connect(options[, callback])`](https://nodejs.org/dist/latest-v8.x/docs/api/tls.html#tls_tls_connect_options_callback).
|
|
138
|
+
|
|
139
|
+
Phin parses a `url` and combines it with any options you want. This behavior can be very useful when you need to pass some additional `headers`. Also, you can pass `rejectUnauthorized: false` if you don't require an SSL certificate to be valid (it helps to prevent `unable to verify the first certificate` error).
|
|
140
|
+
|
|
141
|
+
The convenience method `Jimp.create` also exists. It is just a wrapper around `Jimp.read`.
|
|
120
142
|
|
|
121
143
|
### Custom Constructor
|
|
122
144
|
|
package/browser/lib/jimp.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
Jimp v0.5.
|
|
2
|
+
Jimp v0.5.5
|
|
3
3
|
https://github.com/oliver-moran/jimp
|
|
4
4
|
Ported for the Web by Phil Seaton
|
|
5
5
|
MIT License
|
|
@@ -21707,7 +21707,169 @@ var substr = 'ab'.substr(-1) === 'b'
|
|
|
21707
21707
|
}).call(this,require('_process'))
|
|
21708
21708
|
},{"_process":223}],198:[function(require,module,exports){
|
|
21709
21709
|
(function (Buffer){
|
|
21710
|
-
'use strict';
|
|
21710
|
+
'use strict';
|
|
21711
|
+
var _typeof =
|
|
21712
|
+
typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'
|
|
21713
|
+
? function(obj) {
|
|
21714
|
+
return typeof obj;
|
|
21715
|
+
}
|
|
21716
|
+
: function(obj) {
|
|
21717
|
+
return obj &&
|
|
21718
|
+
typeof Symbol === 'function' &&
|
|
21719
|
+
obj.constructor === Symbol &&
|
|
21720
|
+
obj !== Symbol.prototype
|
|
21721
|
+
? 'symbol'
|
|
21722
|
+
: typeof obj;
|
|
21723
|
+
};
|
|
21724
|
+
var http = require('http');
|
|
21725
|
+
var https = require('https');
|
|
21726
|
+
var url = require('url');
|
|
21727
|
+
var qs = require('querystring');
|
|
21728
|
+
var zlib = require('zlib');
|
|
21729
|
+
var util = require('util');
|
|
21730
|
+
var phin = function phin(opts, cb) {
|
|
21731
|
+
if (typeof opts !== 'string') {
|
|
21732
|
+
if (!opts.hasOwnProperty('url')) {
|
|
21733
|
+
throw new Error('Missing url option from options for request method.');
|
|
21734
|
+
}
|
|
21735
|
+
}
|
|
21736
|
+
var addr =
|
|
21737
|
+
(typeof opts === 'undefined' ? 'undefined' : _typeof(opts)) === 'object'
|
|
21738
|
+
? url.parse(opts.url)
|
|
21739
|
+
: url.parse(opts);
|
|
21740
|
+
var options = {
|
|
21741
|
+
hostname: addr.hostname,
|
|
21742
|
+
port: addr.port || (addr.protocol.toLowerCase() === 'http:' ? 80 : 443),
|
|
21743
|
+
path: addr.path,
|
|
21744
|
+
method: 'GET',
|
|
21745
|
+
headers: {},
|
|
21746
|
+
auth: addr.auth || null,
|
|
21747
|
+
parse: 'none',
|
|
21748
|
+
stream: false
|
|
21749
|
+
};
|
|
21750
|
+
if (
|
|
21751
|
+
(typeof opts === 'undefined' ? 'undefined' : _typeof(opts)) === 'object'
|
|
21752
|
+
) {
|
|
21753
|
+
options = Object.assign(options, opts);
|
|
21754
|
+
}
|
|
21755
|
+
options.port = Number(options.port);
|
|
21756
|
+
if (options.hasOwnProperty('timeout')) delete options.timeout;
|
|
21757
|
+
if (options.compressed === true) {
|
|
21758
|
+
options.headers['accept-encoding'] = 'gzip, deflate';
|
|
21759
|
+
}
|
|
21760
|
+
if (opts.hasOwnProperty('form')) {
|
|
21761
|
+
if (_typeof(opts.form) !== 'object') {
|
|
21762
|
+
throw new Error("phin 'form' option must be of type Object if present.");
|
|
21763
|
+
}
|
|
21764
|
+
var formDataString = qs.stringify(opts.form);
|
|
21765
|
+
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
21766
|
+
options.headers['Content-Length'] = Buffer.byteLength(formDataString);
|
|
21767
|
+
opts.data = formDataString;
|
|
21768
|
+
}
|
|
21769
|
+
var req = void 0;
|
|
21770
|
+
var resHandler = function resHandler(res) {
|
|
21771
|
+
var stream = res;
|
|
21772
|
+
if (options.compressed === true) {
|
|
21773
|
+
if (res.headers['content-encoding'] === 'gzip') {
|
|
21774
|
+
stream = res.pipe(zlib.createGunzip());
|
|
21775
|
+
} else if (res.headers['content-encoding'] === 'deflate') {
|
|
21776
|
+
stream = res.pipe(zlib.createInflate());
|
|
21777
|
+
}
|
|
21778
|
+
}
|
|
21779
|
+
if (options.stream === true) {
|
|
21780
|
+
res.stream = stream;
|
|
21781
|
+
cb(null, res);
|
|
21782
|
+
} else {
|
|
21783
|
+
res.body = new Buffer([]);
|
|
21784
|
+
stream.on('data', function(chunk) {
|
|
21785
|
+
res.body = Buffer.concat([res.body, chunk]);
|
|
21786
|
+
});
|
|
21787
|
+
stream.on('end', function() {
|
|
21788
|
+
if (cb) {
|
|
21789
|
+
if (options.parse === 'json') {
|
|
21790
|
+
try {
|
|
21791
|
+
res.body = JSON.parse(res.body.toString());
|
|
21792
|
+
} catch (err) {
|
|
21793
|
+
cb('Invalid JSON received.', res);
|
|
21794
|
+
return;
|
|
21795
|
+
}
|
|
21796
|
+
}
|
|
21797
|
+
cb(null, res);
|
|
21798
|
+
}
|
|
21799
|
+
});
|
|
21800
|
+
}
|
|
21801
|
+
};
|
|
21802
|
+
switch (addr.protocol.toLowerCase()) {
|
|
21803
|
+
case 'http:':
|
|
21804
|
+
req = http.request(options, resHandler);
|
|
21805
|
+
break;
|
|
21806
|
+
case 'https:':
|
|
21807
|
+
req = https.request(options, resHandler);
|
|
21808
|
+
break;
|
|
21809
|
+
default:
|
|
21810
|
+
if (cb) {
|
|
21811
|
+
cb(
|
|
21812
|
+
new Error('Invalid / unknown URL protocol. Expected HTTP or HTTPS.'),
|
|
21813
|
+
null
|
|
21814
|
+
);
|
|
21815
|
+
}
|
|
21816
|
+
return;
|
|
21817
|
+
}
|
|
21818
|
+
if (typeof opts.timeout === 'number') {
|
|
21819
|
+
req.setTimeout(opts.timeout, function() {
|
|
21820
|
+
req.abort();
|
|
21821
|
+
cb(new Error('Timeout has been reached.'), null);
|
|
21822
|
+
cb = null;
|
|
21823
|
+
});
|
|
21824
|
+
}
|
|
21825
|
+
req.on('error', function(err) {
|
|
21826
|
+
if (cb) {
|
|
21827
|
+
cb(err, null);
|
|
21828
|
+
}
|
|
21829
|
+
});
|
|
21830
|
+
if (opts.hasOwnProperty('data')) {
|
|
21831
|
+
var postData = opts.data;
|
|
21832
|
+
if (!(opts.data instanceof Buffer) && _typeof(opts.data) === 'object') {
|
|
21833
|
+
var contentType =
|
|
21834
|
+
options.headers['content-type'] || options.headers['Content-Type'];
|
|
21835
|
+
if (contentType === 'application/x-www-form-urlencoded') {
|
|
21836
|
+
postData = qs.stringify(opts.data);
|
|
21837
|
+
} else {
|
|
21838
|
+
try {
|
|
21839
|
+
postData = JSON.stringify(opts.data);
|
|
21840
|
+
} catch (err) {
|
|
21841
|
+
cb(
|
|
21842
|
+
new Error(
|
|
21843
|
+
"Couldn't stringify object. (Likely due to a circular reference.)"
|
|
21844
|
+
),
|
|
21845
|
+
null
|
|
21846
|
+
);
|
|
21847
|
+
}
|
|
21848
|
+
}
|
|
21849
|
+
}
|
|
21850
|
+
req.write(postData);
|
|
21851
|
+
}
|
|
21852
|
+
req.end();
|
|
21853
|
+
};
|
|
21854
|
+
phin.promisified = function(opts, http) {
|
|
21855
|
+
return new Promise(function(resolve, reject) {
|
|
21856
|
+
phin(
|
|
21857
|
+
opts,
|
|
21858
|
+
function(err, res) {
|
|
21859
|
+
if (err) {
|
|
21860
|
+
reject(err);
|
|
21861
|
+
} else {
|
|
21862
|
+
resolve(res);
|
|
21863
|
+
}
|
|
21864
|
+
},
|
|
21865
|
+
http
|
|
21866
|
+
);
|
|
21867
|
+
});
|
|
21868
|
+
};
|
|
21869
|
+
if (util.promisify) {
|
|
21870
|
+
phin[util.promisify.custom] = phin.promisified;
|
|
21871
|
+
}
|
|
21872
|
+
module.exports = phin;
|
|
21711
21873
|
|
|
21712
21874
|
}).call(this,require("buffer").Buffer)
|
|
21713
21875
|
},{"buffer":16,"http":243,"https":164,"querystring":227,"url":253,"util":258,"zlib":13}],199:[function(require,module,exports){
|
|
@@ -34412,9 +34574,9 @@ function jimpEvMethod(methodName, evName, method) {
|
|
|
34412
34574
|
if (!wrappedCb) {
|
|
34413
34575
|
this.emitMulti(methodName, evNameAfter, _defineProperty({}, methodName, result));
|
|
34414
34576
|
}
|
|
34415
|
-
} catch (
|
|
34416
|
-
|
|
34417
|
-
this.emitError(methodName,
|
|
34577
|
+
} catch (error) {
|
|
34578
|
+
error.methodName = methodName;
|
|
34579
|
+
this.emitError(methodName, error);
|
|
34418
34580
|
}
|
|
34419
34581
|
|
|
34420
34582
|
return result;
|
|
@@ -34733,8 +34895,8 @@ if (process.browser || "BROWSER" === 'BROWSER' || typeof process.versions.electr
|
|
|
34733
34895
|
try {
|
|
34734
34896
|
var data = Buffer.from(this.response);
|
|
34735
34897
|
cb(null, xhr, data);
|
|
34736
|
-
} catch (
|
|
34737
|
-
return cb(new Error('Response is not a buffer for url ' + options.url + '. Error: ' +
|
|
34898
|
+
} catch (error) {
|
|
34899
|
+
return cb(new Error('Response is not a buffer for url ' + options.url + '. Error: ' + error.message));
|
|
34738
34900
|
}
|
|
34739
34901
|
} else {
|
|
34740
34902
|
cb(new Error('HTTP Status ' + xhr.status + ' for url ' + options.url));
|
|
@@ -34893,7 +35055,7 @@ function parseBitmap(data, path, cb) {
|
|
|
34893
35055
|
try {
|
|
34894
35056
|
this._exif = _exifParser.default.create(data).parse();
|
|
34895
35057
|
exifRotate(this); // EXIF data
|
|
34896
|
-
} catch (
|
|
35058
|
+
} catch (error) {
|
|
34897
35059
|
/* meh */
|
|
34898
35060
|
}
|
|
34899
35061
|
|
|
@@ -35616,9 +35778,9 @@ function colorFn(actions, cb) {
|
|
|
35616
35778
|
b: _this.bitmap.data[idx + 2]
|
|
35617
35779
|
};
|
|
35618
35780
|
|
|
35619
|
-
function colorModifier(i, amount) {
|
|
35620
|
-
return
|
|
35621
|
-
}
|
|
35781
|
+
var colorModifier = function colorModifier(i, amount) {
|
|
35782
|
+
return _this.constructor.limit255(clr[i] + amount);
|
|
35783
|
+
};
|
|
35622
35784
|
|
|
35623
35785
|
actions.forEach(function (action) {
|
|
35624
35786
|
if (action.apply === 'mix') {
|
|
@@ -35657,7 +35819,7 @@ function colorFn(actions, cb) {
|
|
|
35657
35819
|
clr = (0, _tinycolor.default)(clr);
|
|
35658
35820
|
|
|
35659
35821
|
if (!clr[action.apply]) {
|
|
35660
|
-
return _utils.throwError.call(
|
|
35822
|
+
return _utils.throwError.call(_this, 'action ' + action.apply + ' not supported', cb);
|
|
35661
35823
|
}
|
|
35662
35824
|
|
|
35663
35825
|
clr = (_clr = clr)[action.apply].apply(_clr, _toConsumableArray(action.params)).toRgb();
|
|
@@ -36030,7 +36192,7 @@ var _default = function _default() {
|
|
|
36030
36192
|
var kernel = [[1 / 16, 2 / 16, 1 / 16], [2 / 16, 4 / 16, 2 / 16], [1 / 16, 2 / 16, 1 / 16]];
|
|
36031
36193
|
x = x || 0;
|
|
36032
36194
|
y = y || 0;
|
|
36033
|
-
w = isDef(w) ?
|
|
36195
|
+
w = isDef(w) ? w : this.bitmap.width - x;
|
|
36034
36196
|
h = isDef(h) ? h : this.bitmap.height - y;
|
|
36035
36197
|
var source = this.cloneQuiet();
|
|
36036
36198
|
this.scanQuiet(x, y, w, h, function (xx, yx, idx) {
|