-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
215 lines (155 loc) · 5.52 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'use strict';
var utils = require('./lib/utils');
var pica = require('pica');
function ImageBlobReduce(options) {
if (!(this instanceof ImageBlobReduce)) return new ImageBlobReduce(options);
options = options || {};
this.pica = options.pica || pica({});
this.initialized = false;
this.utils = utils;
}
ImageBlobReduce.prototype.use = function (plugin /*, params, ... */) {
var args = [ this ].concat(Array.prototype.slice.call(arguments, 1));
plugin.apply(plugin, args);
return this;
};
ImageBlobReduce.prototype.init = function () {
this.use(require('./lib/jpeg_plugins').assign);
};
ImageBlobReduce.prototype.toBlob = function (blob, options) {
var opts = utils.assign({ max: Infinity }, options);
var env = {
blob: blob,
opts: opts
};
if (!this.initialized) {
this.init();
this.initialized = true;
}
return Promise.resolve(env)
.then(this._blob_to_image)
.then(this._calculate_size)
.then(this._transform)
.then(this._cleanup)
.then(this._create_blob)
.then(function (_env) {
// Safari 12 workaround
// https://github.com/nodeca/pica/issues/199
_env.out_canvas.width = _env.out_canvas.height = 0;
return _env.out_blob;
});
};
ImageBlobReduce.prototype.toCanvas = function (blob, options) {
var opts = utils.assign({ max: Infinity }, options);
var env = {
blob: blob,
opts: opts
};
if (!this.initialized) {
this.init();
this.initialized = true;
}
return Promise.resolve(env)
.then(this._blob_to_image)
.then(this._calculate_size)
.then(this._transform)
.then(this._cleanup)
.then(function (_env) { return _env.out_canvas; });
};
ImageBlobReduce.prototype.before = function (method_name, fn) {
if (!this[method_name]) throw new Error('Method "' + method_name + '" does not exist');
if (typeof fn !== 'function') throw new Error('Invalid argument "fn", function expected');
var old_fn = this[method_name];
var self = this;
this[method_name] = function (env) {
return fn.call(self, env).then(function (_env) {
return old_fn.call(self, _env);
});
};
return this;
};
ImageBlobReduce.prototype.after = function (method_name, fn) {
if (!this[method_name]) throw new Error('Method "' + method_name + '" does not exist');
if (typeof fn !== 'function') throw new Error('Invalid argument "fn", function expected');
var old_fn = this[method_name];
var self = this;
this[method_name] = function (env) {
return old_fn.call(self, env).then(function (_env) {
return fn.call(self, _env);
});
};
return this;
};
ImageBlobReduce.prototype._blob_to_image = function (env) {
var URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
env.image = document.createElement('img');
env.image_url = URL.createObjectURL(env.blob);
env.image.src = env.image_url;
return new Promise(function (resolve, reject) {
env.image.onerror = function () { reject(new Error('ImageBlobReduce: failed to create Image() from blob')); };
env.image.onload = function () { resolve(env); };
});
};
ImageBlobReduce.prototype._calculate_size = function (env) {
//
// Note, if your need not "symmetric" resize logic, you MUST check
// `env.orientation` (set by plugins) and swap width/height appropriately.
//
var scale_factor = env.opts.max / Math.max(env.image.width, env.image.height);
if (scale_factor > 1) scale_factor = 1;
env.transform_width = Math.max(Math.round(env.image.width * scale_factor), 1);
env.transform_height = Math.max(Math.round(env.image.height * scale_factor), 1);
// Info for user plugins, to check if scaling applied
env.scale_factor = scale_factor;
return Promise.resolve(env);
};
ImageBlobReduce.prototype._transform = function (env) {
env.out_canvas = this.pica.options.createCanvas(env.transform_width, env.transform_height);
// Dim env temporary vars to prohibit use and avoid confusion when orientation
// changed. You should take real size from canvas.
env.transform_width = null;
env.transform_height = null;
// By default use alpha for png only
var pica_opts = { alpha: env.blob.type === 'image/png' };
// Extract pica options if been passed
this.utils.assign(pica_opts, this.utils.pick_pica_resize_options(env.opts));
return this.pica
.resize(env.image, env.out_canvas, pica_opts)
.then(function () { return env; });
};
ImageBlobReduce.prototype._cleanup = function (env) {
env.image.src = '';
env.image = null;
var URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
if (URL.revokeObjectURL) URL.revokeObjectURL(env.image_url);
env.image_url = null;
return Promise.resolve(env);
};
ImageBlobReduce.prototype._create_blob = function (env) {
return this.pica.toBlob(env.out_canvas, env.blob.type)
.then(function (blob) {
env.out_blob = blob;
return env;
});
};
ImageBlobReduce.prototype._getUint8Array = function (blob) {
if (blob.arrayBuffer) {
return blob.arrayBuffer().then(function (buf) {
return new Uint8Array(buf);
});
}
return new Promise(function (resolve, reject) {
var fr = new FileReader();
fr.readAsArrayBuffer(blob);
fr.onload = function () { resolve(new Uint8Array(fr.result)); };
fr.onerror = function () {
reject(new Error('ImageBlobReduce: failed to load data from input blob'));
fr.abort();
};
fr.onabort = function () {
reject(new Error('ImageBlobReduce: failed to load data from input blob (aborted)'));
};
});
};
ImageBlobReduce.pica = pica;
module.exports = ImageBlobReduce;