@pi-r/jimp 0.3.1 → 0.6.0
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/index.js +87 -92
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -73,9 +73,9 @@ function getMethodName(value) {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
|
-
function performCommand(host, instance, localUri, command, outputType, finalAs, buffer, parent) {
|
|
76
|
+
async function performCommand(host, instance, localUri, command, outputType, finalAs, buffer, parent) {
|
|
77
77
|
return jimp.read((buffer || localUri))
|
|
78
|
-
.then(img => {
|
|
78
|
+
.then(async (img) => {
|
|
79
79
|
return transformCommand(localUri, new JimpHandler(img, instance, host), command, outputType, finalAs, parent);
|
|
80
80
|
});
|
|
81
81
|
}
|
|
@@ -275,32 +275,31 @@ class JimpHandler {
|
|
|
275
275
|
return;
|
|
276
276
|
}
|
|
277
277
|
const data = this.instance.methodData;
|
|
278
|
-
if (
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
278
|
+
if (data) {
|
|
279
|
+
const handler = this.handler;
|
|
280
|
+
for (const [name, args = []] of data) {
|
|
281
|
+
try {
|
|
282
|
+
const alias = getMethodName(name);
|
|
283
|
+
if (!alias) {
|
|
284
|
+
throw (0, types_1.errorValue)("Invalid method name" /* ERR_IMAGE.METHOD_NAME */, name);
|
|
285
|
+
}
|
|
286
|
+
if (alias === 'composite') {
|
|
287
|
+
const [src, x, y, opts] = args;
|
|
288
|
+
if ((0, types_1.isString)(src) && typeof x === 'number' && typeof y === 'number') {
|
|
289
|
+
handler.composite(await jimp.read(src), x, y, opts);
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
throw (0, types_1.errorValue)("Invalid parameters" /* ERR_MESSAGE.PARAMETERS */, alias);
|
|
293
|
+
}
|
|
292
294
|
}
|
|
293
295
|
else {
|
|
294
|
-
|
|
296
|
+
handler[alias](...args);
|
|
295
297
|
}
|
|
296
298
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
+
catch (err) {
|
|
300
|
+
this.instance.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, "jimp" /* STRINGS.MODULE_NAME */ + ': ' + name], err, 2048 /* LOG_TYPE.IMAGE */);
|
|
299
301
|
}
|
|
300
302
|
}
|
|
301
|
-
catch (err) {
|
|
302
|
-
this.instance.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, "jimp" /* STRINGS.MODULE_NAME */ + ': ' + name], err, 2048 /* LOG_TYPE.IMAGE */);
|
|
303
|
-
}
|
|
304
303
|
}
|
|
305
304
|
}
|
|
306
305
|
resize() {
|
|
@@ -308,64 +307,63 @@ class JimpHandler {
|
|
|
308
307
|
return;
|
|
309
308
|
}
|
|
310
309
|
const data = this.instance.resizeData;
|
|
311
|
-
if (
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
break;
|
|
310
|
+
if (data) {
|
|
311
|
+
const { width, height, color, algorithm, align, mode } = data;
|
|
312
|
+
const handler = this.handler;
|
|
313
|
+
if (!isNaN(color)) {
|
|
314
|
+
handler.background(color);
|
|
315
|
+
}
|
|
316
|
+
let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR, flags = 0;
|
|
317
|
+
switch (algorithm) {
|
|
318
|
+
case 'bilinear':
|
|
319
|
+
resizeMode = jimp.RESIZE_BILINEAR;
|
|
320
|
+
break;
|
|
321
|
+
case 'bicubic':
|
|
322
|
+
resizeMode = jimp.RESIZE_BICUBIC;
|
|
323
|
+
break;
|
|
324
|
+
case 'hermite':
|
|
325
|
+
resizeMode = jimp.RESIZE_HERMITE;
|
|
326
|
+
break;
|
|
327
|
+
case 'bezier':
|
|
328
|
+
resizeMode = jimp.RESIZE_BEZIER;
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
switch (align[0]) {
|
|
332
|
+
case 'left':
|
|
333
|
+
flags |= jimp.HORIZONTAL_ALIGN_LEFT;
|
|
334
|
+
break;
|
|
335
|
+
case 'center':
|
|
336
|
+
flags |= jimp.HORIZONTAL_ALIGN_CENTER;
|
|
337
|
+
break;
|
|
338
|
+
case 'right':
|
|
339
|
+
flags |= jimp.HORIZONTAL_ALIGN_RIGHT;
|
|
340
|
+
break;
|
|
341
|
+
}
|
|
342
|
+
switch (align[1]) {
|
|
343
|
+
case 'top':
|
|
344
|
+
flags |= jimp.VERTICAL_ALIGN_TOP;
|
|
345
|
+
break;
|
|
346
|
+
case 'middle':
|
|
347
|
+
flags |= jimp.VERTICAL_ALIGN_MIDDLE;
|
|
348
|
+
break;
|
|
349
|
+
case 'bottom':
|
|
350
|
+
flags |= jimp.VERTICAL_ALIGN_BOTTOM;
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
switch (mode) {
|
|
354
|
+
case 'contain':
|
|
355
|
+
handler.contain(width, height, flags);
|
|
356
|
+
break;
|
|
357
|
+
case 'cover':
|
|
358
|
+
handler.cover(width, height, flags);
|
|
359
|
+
break;
|
|
360
|
+
case 'scale':
|
|
361
|
+
handler.scaleToFit(width, height);
|
|
362
|
+
break;
|
|
363
|
+
default:
|
|
364
|
+
handler.resize(width === Infinity ? jimp.AUTO : width, height === Infinity ? jimp.AUTO : height, resizeMode);
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
369
367
|
}
|
|
370
368
|
}
|
|
371
369
|
background(value) {
|
|
@@ -383,7 +381,7 @@ class JimpHandler {
|
|
|
383
381
|
const settings = instance.settings;
|
|
384
382
|
const webp = settings.webp || (settings.webp = {});
|
|
385
383
|
const data = instance.qualityData;
|
|
386
|
-
const replace = instance.getCommand().
|
|
384
|
+
const replace = instance.getCommand().includes('@');
|
|
387
385
|
const filename = (0, util_1.renameExt)(output, 'webp', replace);
|
|
388
386
|
const args = [(0, util_1.normalizePath)(output)];
|
|
389
387
|
if (data) {
|
|
@@ -455,10 +453,10 @@ class JimpHandler {
|
|
|
455
453
|
callback(null, output);
|
|
456
454
|
}
|
|
457
455
|
}
|
|
458
|
-
getBuffer(tempFile, saveAs) {
|
|
456
|
+
async getBuffer(tempFile, saveAs) {
|
|
459
457
|
const empty = () => tempFile ? '' : null;
|
|
460
458
|
const output = getTempPath.call(this.instance, this.instance.outputAs || (saveAs && util_1.MIME_OUTPUT.has('image/' + (saveAs === 'jpg' ? 'jpeg' : saveAs)) ? saveAs : this.handler.getMIME().split('/').pop()));
|
|
461
|
-
return !output ?
|
|
459
|
+
return !output ? empty() : new Promise(resolve => {
|
|
462
460
|
this.handler.write(output, error => {
|
|
463
461
|
if (error) {
|
|
464
462
|
resolve(empty());
|
|
@@ -528,12 +526,12 @@ class JimpHandler {
|
|
|
528
526
|
}
|
|
529
527
|
});
|
|
530
528
|
}
|
|
531
|
-
writeAsync(output, callback) {
|
|
529
|
+
async writeAsync(output, callback) {
|
|
532
530
|
if (this.aborted) {
|
|
533
531
|
if (callback) {
|
|
534
532
|
callback((0, types_1.createAbortError)(), '');
|
|
535
533
|
}
|
|
536
|
-
return
|
|
534
|
+
return;
|
|
537
535
|
}
|
|
538
536
|
return this.handler.writeAsync(output)
|
|
539
537
|
.then(() => this.finalize(output, callback))
|
|
@@ -610,7 +608,7 @@ class Jimp extends Image {
|
|
|
610
608
|
[buffer, tempFile, ctimeMs] = getImageCache.call(instance, tempKey = file + command + (options.mimeType || ''));
|
|
611
609
|
if (buffer) {
|
|
612
610
|
writeMessage(false, ctimeMs);
|
|
613
|
-
return
|
|
611
|
+
return buffer;
|
|
614
612
|
}
|
|
615
613
|
}
|
|
616
614
|
instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp" /* STRINGS.MODULE_NAME */, ["Transforming image..." /* STRINGS.TRANSFORM */, filename], command);
|
|
@@ -659,7 +657,7 @@ class Jimp extends Image {
|
|
|
659
657
|
reject((0, types_1.errorValue)("Invalid format" /* ERR_MESSAGE.FORMAT */, /^\w+/.exec(command)?.[0] || "Unknown" /* ERR_MESSAGE.UNKNOWN */));
|
|
660
658
|
return;
|
|
661
659
|
}
|
|
662
|
-
const replace = command.
|
|
660
|
+
const replace = command.includes('@');
|
|
663
661
|
const output = host.addCopy(data.getObject({ command, outputType }), saveAs, replace);
|
|
664
662
|
if (!output) {
|
|
665
663
|
reject((0, types_1.errorValue)("Not able to copy file" /* ERR_MESSAGE.NOT_COPYABLE */, outputType));
|
|
@@ -677,7 +675,7 @@ class Jimp extends Image {
|
|
|
677
675
|
host.writeImage(document, data.getObject({ command, output: result }));
|
|
678
676
|
}
|
|
679
677
|
if (host.getLocalUri(data) !== result) {
|
|
680
|
-
if (command.
|
|
678
|
+
if (command.includes('%')) {
|
|
681
679
|
const files = host.filesToCompare.get(file);
|
|
682
680
|
if (files) {
|
|
683
681
|
files.push(result);
|
|
@@ -852,7 +850,7 @@ class Jimp extends Image {
|
|
|
852
850
|
GifUtil.read(file.buffer || localUri)
|
|
853
851
|
.then(gif => {
|
|
854
852
|
rotateAnim(cmd);
|
|
855
|
-
Promise.all(gif.frames.map(frame => {
|
|
853
|
+
Promise.all(gif.frames.map(async (frame) => {
|
|
856
854
|
const handler = new JimpHandler(GifUtil.shareAsJimp(jimp, frame), this);
|
|
857
855
|
return transformCommand(localUri, handler, cmd, jimp.MIME_GIF);
|
|
858
856
|
}))
|
|
@@ -889,9 +887,6 @@ class Jimp extends Image {
|
|
|
889
887
|
try {
|
|
890
888
|
const webp = new (require('node-webpmux').Image)();
|
|
891
889
|
loaded = true;
|
|
892
|
-
if (Image.supported(18, 1)) {
|
|
893
|
-
this.formatMessage(2048 /* LOG_TYPE.IMAGE */, 'WARN', 'node-webpmux is not compatible with global fetch', 'NodeJS: --no-experimental-fetch', { ...Image.LOG_STYLE_WARN });
|
|
894
|
-
}
|
|
895
890
|
await webp.initLib();
|
|
896
891
|
await webp.load(host.getBuffer(file));
|
|
897
892
|
if (!(webp.hasAnim && (outputType === "image/webp" /* STRINGS.MIME_WEBP */ || outputType === jimp.MIME_GIF))) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/jimp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Jimp image constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/image": "^0.
|
|
24
|
-
"@e-mc/types": "^0.
|
|
23
|
+
"@e-mc/image": "^0.8.0",
|
|
24
|
+
"@e-mc/types": "^0.8.0",
|
|
25
25
|
"bmp-js": "^0.1.0",
|
|
26
26
|
"gifwrap": "^0.10.1",
|
|
27
27
|
"jimp": "^0.22.10"
|