@pi-r/jimp 0.9.3 → 0.10.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.d.ts +7 -0
- package/index.js +76 -74
- package/package.json +4 -3
- package/types/index.d.ts +1 -2
- package/util.d.ts +2 -0
- package/util.js +27 -23
package/index.d.ts
ADDED
package/index.js
CHANGED
|
@@ -9,6 +9,7 @@ const node_crypto_1 = require("node:crypto");
|
|
|
9
9
|
const types_1 = require("@e-mc/types");
|
|
10
10
|
const util_1 = require("@pi-r/jimp/util");
|
|
11
11
|
const Image = require('@e-mc/image');
|
|
12
|
+
const kJimp = Symbol.for('jimp:constructor');
|
|
12
13
|
let WEBPMUX = null, WEBPMUX_INIT = false;
|
|
13
14
|
try {
|
|
14
15
|
WEBPMUX = require('node-webpmux');
|
|
@@ -80,7 +81,7 @@ function getMethodName(value) {
|
|
|
80
81
|
return value;
|
|
81
82
|
}
|
|
82
83
|
}
|
|
83
|
-
async function performCommand(host, instance, localUri, command, outputType, outputAs, buffer, parent) {
|
|
84
|
+
async function performCommand(host, instance, localUri, command, outputType, outputAs, { buffer, parent } = {}) {
|
|
84
85
|
return await jimp.read((buffer || localUri))
|
|
85
86
|
.then(async (img) => {
|
|
86
87
|
return await transformCommand(localUri, new JimpHandler(img, instance, host), command, outputType, outputAs, parent);
|
|
@@ -145,8 +146,8 @@ async function setImageCache(instance, tempKey, tempFile, output, localFile) {
|
|
|
145
146
|
else {
|
|
146
147
|
stored[tempKey] = { tempKey, tempFile, ctimeMs: Date.now() };
|
|
147
148
|
}
|
|
148
|
-
if (instance.settings.jimp
|
|
149
|
-
fs.writeFile(tempFile + '.json', JSON.stringify(stored[tempKey]), '
|
|
149
|
+
if (instance.settings.jimp?.cache_expires) {
|
|
150
|
+
fs.writeFile(tempFile + '.json', JSON.stringify(stored[tempKey]), 'utf8', () => { });
|
|
150
151
|
}
|
|
151
152
|
}
|
|
152
153
|
catch {
|
|
@@ -195,7 +196,7 @@ function getCacheData(instance) {
|
|
|
195
196
|
if (item.isFile() && path.extname(item.name) === '.json') {
|
|
196
197
|
const pathname = path.join(TEMP_DIR, item.name);
|
|
197
198
|
try {
|
|
198
|
-
const data = JSON.parse(fs.readFileSync(pathname, '
|
|
199
|
+
const data = JSON.parse(fs.readFileSync(pathname, 'utf8'));
|
|
199
200
|
if ((0, types_1.isPlainObject)(data) && fs.existsSync(data.tempFile)) {
|
|
200
201
|
if (data.ctimeMs + expires > current) {
|
|
201
202
|
CACHE_TRANSFORM[data.tempKey] = data;
|
|
@@ -257,9 +258,8 @@ class JimpHandler {
|
|
|
257
258
|
const data = this.instance.rotateData;
|
|
258
259
|
if (data) {
|
|
259
260
|
const { values, color } = data;
|
|
260
|
-
const handler = this.handler;
|
|
261
261
|
if (!isNaN(color)) {
|
|
262
|
-
handler.background(color);
|
|
262
|
+
this.handler.background(color);
|
|
263
263
|
}
|
|
264
264
|
const tasks = [];
|
|
265
265
|
const length = values.length;
|
|
@@ -269,11 +269,11 @@ class JimpHandler {
|
|
|
269
269
|
const ext = path.extname(localFile);
|
|
270
270
|
for (let i = 1; i < length; ++i) {
|
|
271
271
|
const value = values[i];
|
|
272
|
-
const img = handler.clone().rotate(value);
|
|
272
|
+
const img = this.handler.clone().rotate(value);
|
|
273
273
|
const output = leading + value + ext;
|
|
274
274
|
tasks.push(img.writeAsync(output)
|
|
275
275
|
.then(() => {
|
|
276
|
-
this.finalize(output, callback);
|
|
276
|
+
void this.finalize(output, callback, false);
|
|
277
277
|
})
|
|
278
278
|
.catch((err) => {
|
|
279
279
|
this.instance.writeFail(["Unable to rotate image", "jimp"], err, 2048);
|
|
@@ -281,7 +281,7 @@ class JimpHandler {
|
|
|
281
281
|
}
|
|
282
282
|
}
|
|
283
283
|
if (deg) {
|
|
284
|
-
handler.rotate(deg);
|
|
284
|
+
this.handler.rotate(deg);
|
|
285
285
|
}
|
|
286
286
|
if (tasks.length > 0) {
|
|
287
287
|
return Promise.all(tasks).then(() => this);
|
|
@@ -295,7 +295,6 @@ class JimpHandler {
|
|
|
295
295
|
}
|
|
296
296
|
const data = this.instance.methodData;
|
|
297
297
|
if (data) {
|
|
298
|
-
const handler = this.handler;
|
|
299
298
|
for (const [name, args = []] of data) {
|
|
300
299
|
try {
|
|
301
300
|
const alias = getMethodName(name);
|
|
@@ -305,14 +304,14 @@ class JimpHandler {
|
|
|
305
304
|
if (alias === 'composite') {
|
|
306
305
|
const [src, x, y, opts] = args;
|
|
307
306
|
if ((0, types_1.isString)(src) && typeof x === 'number' && typeof y === 'number') {
|
|
308
|
-
handler.composite(await jimp.read(src), x, y, opts);
|
|
307
|
+
this.handler.composite(await jimp.read(src), x, y, opts);
|
|
309
308
|
}
|
|
310
309
|
else {
|
|
311
310
|
throw (0, types_1.errorValue)("Invalid parameters", alias);
|
|
312
311
|
}
|
|
313
312
|
}
|
|
314
313
|
else {
|
|
315
|
-
handler[alias](...args);
|
|
314
|
+
this.handler[alias](...args);
|
|
316
315
|
}
|
|
317
316
|
}
|
|
318
317
|
catch (err) {
|
|
@@ -328,25 +327,10 @@ class JimpHandler {
|
|
|
328
327
|
const data = this.instance.resizeData;
|
|
329
328
|
if (data) {
|
|
330
329
|
const { width, height, color, algorithm, align, mode } = data;
|
|
331
|
-
const handler = this.handler;
|
|
332
330
|
if (!isNaN(color)) {
|
|
333
|
-
handler.background(color);
|
|
334
|
-
}
|
|
335
|
-
let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR, flags = 0;
|
|
336
|
-
switch (algorithm) {
|
|
337
|
-
case 'bilinear':
|
|
338
|
-
resizeMode = jimp.RESIZE_BILINEAR;
|
|
339
|
-
break;
|
|
340
|
-
case 'bicubic':
|
|
341
|
-
resizeMode = jimp.RESIZE_BICUBIC;
|
|
342
|
-
break;
|
|
343
|
-
case 'hermite':
|
|
344
|
-
resizeMode = jimp.RESIZE_HERMITE;
|
|
345
|
-
break;
|
|
346
|
-
case 'bezier':
|
|
347
|
-
resizeMode = jimp.RESIZE_BEZIER;
|
|
348
|
-
break;
|
|
331
|
+
this.handler.background(color);
|
|
349
332
|
}
|
|
333
|
+
let flags = 0;
|
|
350
334
|
switch (align[0]) {
|
|
351
335
|
case 'left':
|
|
352
336
|
flags |= jimp.HORIZONTAL_ALIGN_LEFT;
|
|
@@ -371,17 +355,36 @@ class JimpHandler {
|
|
|
371
355
|
}
|
|
372
356
|
switch (mode) {
|
|
373
357
|
case 'contain':
|
|
374
|
-
handler.contain(width, height, flags);
|
|
358
|
+
this.handler.contain(width, height, flags);
|
|
375
359
|
break;
|
|
376
360
|
case 'cover':
|
|
377
|
-
handler.cover(width, height, flags);
|
|
361
|
+
this.handler.cover(width, height, flags);
|
|
378
362
|
break;
|
|
379
363
|
case 'scale':
|
|
380
|
-
handler.scaleToFit(width, height);
|
|
364
|
+
this.handler.scaleToFit(width, height);
|
|
381
365
|
break;
|
|
382
|
-
default:
|
|
383
|
-
|
|
366
|
+
default: {
|
|
367
|
+
let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR;
|
|
368
|
+
switch (algorithm) {
|
|
369
|
+
case 'bilinear':
|
|
370
|
+
resizeMode = jimp.RESIZE_BILINEAR;
|
|
371
|
+
break;
|
|
372
|
+
case 'bicubic':
|
|
373
|
+
resizeMode = jimp.RESIZE_BICUBIC;
|
|
374
|
+
break;
|
|
375
|
+
case 'hermite':
|
|
376
|
+
resizeMode = jimp.RESIZE_HERMITE;
|
|
377
|
+
break;
|
|
378
|
+
case 'bezier':
|
|
379
|
+
resizeMode = jimp.RESIZE_BEZIER;
|
|
380
|
+
break;
|
|
381
|
+
case 'none':
|
|
382
|
+
resizeMode = undefined;
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
this.handler.resize(width === Infinity ? jimp.AUTO : width, height === Infinity ? jimp.AUTO : height, resizeMode);
|
|
384
386
|
break;
|
|
387
|
+
}
|
|
385
388
|
}
|
|
386
389
|
}
|
|
387
390
|
}
|
|
@@ -391,7 +394,7 @@ class JimpHandler {
|
|
|
391
394
|
}
|
|
392
395
|
this.handler.background(value);
|
|
393
396
|
}
|
|
394
|
-
finalize(output, callback) {
|
|
397
|
+
async finalize(output, callback, replace) {
|
|
395
398
|
if (this.aborted) {
|
|
396
399
|
return;
|
|
397
400
|
}
|
|
@@ -400,7 +403,7 @@ class JimpHandler {
|
|
|
400
403
|
const settings = instance.settings;
|
|
401
404
|
const webp = settings.webp ||= {};
|
|
402
405
|
const data = instance.qualityData;
|
|
403
|
-
|
|
406
|
+
replace ??= instance.getCommand().includes('@');
|
|
404
407
|
const filename = (0, util_1.renameExt)(output, 'webp', replace);
|
|
405
408
|
const args = [(0, util_1.normalizePath)(output)];
|
|
406
409
|
if (data) {
|
|
@@ -445,7 +448,7 @@ class JimpHandler {
|
|
|
445
448
|
}
|
|
446
449
|
args.push('-o', (0, util_1.normalizePath)(filename));
|
|
447
450
|
try {
|
|
448
|
-
child_process.execFile((0, util_1.
|
|
451
|
+
child_process.execFile(await (0, util_1.importBinary)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
|
|
449
452
|
if (err) {
|
|
450
453
|
this.instance.writeFail(["Unable to convert file", path.basename(filename)], err, 2048);
|
|
451
454
|
}
|
|
@@ -466,7 +469,7 @@ class JimpHandler {
|
|
|
466
469
|
});
|
|
467
470
|
}
|
|
468
471
|
catch (err) {
|
|
469
|
-
this.instance.checkPackage(err, 'cwebp-bin
|
|
472
|
+
this.instance.checkPackage(err, 'cwebp-bin', "Unknown", { type: 2048, passThrough: !!callback });
|
|
470
473
|
if (callback) {
|
|
471
474
|
callback(err, '');
|
|
472
475
|
}
|
|
@@ -488,7 +491,7 @@ class JimpHandler {
|
|
|
488
491
|
resolve(emptyData());
|
|
489
492
|
return;
|
|
490
493
|
}
|
|
491
|
-
this.finalize(output, (error, result) => {
|
|
494
|
+
void this.finalize(output, (error, result) => {
|
|
492
495
|
if (error) {
|
|
493
496
|
resolve(emptyData());
|
|
494
497
|
}
|
|
@@ -521,7 +524,7 @@ class JimpHandler {
|
|
|
521
524
|
}
|
|
522
525
|
const data = this.instance.cropData;
|
|
523
526
|
if (data) {
|
|
524
|
-
this.handler
|
|
527
|
+
this.handler.crop(data.x, data.y, data.width, data.height);
|
|
525
528
|
}
|
|
526
529
|
}
|
|
527
530
|
opacity() {
|
|
@@ -530,7 +533,7 @@ class JimpHandler {
|
|
|
530
533
|
}
|
|
531
534
|
const value = this.instance.opacityValue ?? NaN;
|
|
532
535
|
if (value >= 0) {
|
|
533
|
-
this.handler
|
|
536
|
+
this.handler.opacity(value);
|
|
534
537
|
}
|
|
535
538
|
}
|
|
536
539
|
quality() {
|
|
@@ -539,7 +542,7 @@ class JimpHandler {
|
|
|
539
542
|
}
|
|
540
543
|
const data = this.instance.qualityData;
|
|
541
544
|
if (data && !isNaN(data.value)) {
|
|
542
|
-
this.handler
|
|
545
|
+
this.handler.quality(data.value);
|
|
543
546
|
}
|
|
544
547
|
}
|
|
545
548
|
write(output, callback) {
|
|
@@ -551,7 +554,7 @@ class JimpHandler {
|
|
|
551
554
|
}
|
|
552
555
|
this.handler.write(output, err => {
|
|
553
556
|
if (!err) {
|
|
554
|
-
this.finalize(output, callback);
|
|
557
|
+
void this.finalize(output, callback);
|
|
555
558
|
}
|
|
556
559
|
else if (callback) {
|
|
557
560
|
callback(err, '');
|
|
@@ -567,7 +570,7 @@ class JimpHandler {
|
|
|
567
570
|
}
|
|
568
571
|
return this.handler.writeAsync(output)
|
|
569
572
|
.then(() => {
|
|
570
|
-
this.finalize(output, callback);
|
|
573
|
+
void this.finalize(output, callback);
|
|
571
574
|
})
|
|
572
575
|
.catch((err) => {
|
|
573
576
|
if (callback) {
|
|
@@ -589,6 +592,7 @@ class JimpHandler {
|
|
|
589
592
|
}
|
|
590
593
|
}
|
|
591
594
|
class Jimp extends Image {
|
|
595
|
+
static [kJimp] = true;
|
|
592
596
|
static async transform(file, command, options = {}) {
|
|
593
597
|
const [outputType, saveAs, outputAs] = (0, util_1.parseFormat)(command = command.trim(), options.mimeType);
|
|
594
598
|
if (!outputType) {
|
|
@@ -760,8 +764,8 @@ class Jimp extends Image {
|
|
|
760
764
|
};
|
|
761
765
|
const transformBuffer = (bmpFile) => {
|
|
762
766
|
startMessage();
|
|
763
|
-
performCommand(host, this, localUri, command,
|
|
764
|
-
.then(
|
|
767
|
+
performCommand(host, this, localUri, command, outputType, outputAs, { buffer: bmpFile || file.buffer, parent: file })
|
|
768
|
+
.then(img => {
|
|
765
769
|
if (typeof bmpFile === 'string') {
|
|
766
770
|
removeFile(bmpFile);
|
|
767
771
|
}
|
|
@@ -789,6 +793,9 @@ class Jimp extends Image {
|
|
|
789
793
|
if (!err && result) {
|
|
790
794
|
finalize(result);
|
|
791
795
|
}
|
|
796
|
+
else if (Image.isErrorCode(err, types_1.ERR_CODE.MODULE_NOT_FOUND)) {
|
|
797
|
+
resolve();
|
|
798
|
+
}
|
|
792
799
|
else {
|
|
793
800
|
errorResponse(err || new Error("Unknown"));
|
|
794
801
|
}
|
|
@@ -800,17 +807,13 @@ class Jimp extends Image {
|
|
|
800
807
|
resolve();
|
|
801
808
|
});
|
|
802
809
|
};
|
|
803
|
-
const errorResponse = (err) => {
|
|
804
|
-
this.writeFail(["Unable to convert file", path.basename(localUri)], err, { type: 2048, startTime });
|
|
805
|
-
resolve();
|
|
806
|
-
};
|
|
807
810
|
const startMessage = () => {
|
|
808
811
|
host.formatMessage(2048, "jimp", ["Transforming image...", path.basename(localUri)], command);
|
|
809
812
|
};
|
|
810
813
|
if (mimeType === jimp.MIME_GIF) {
|
|
811
814
|
if (outputType === jimp.MIME_GIF || outputType === "image/webp") {
|
|
812
815
|
const cmd = this.parseCommand(command);
|
|
813
|
-
const transformWebP = (target, modified) => {
|
|
816
|
+
const transformWebP = async (target, modified) => {
|
|
814
817
|
if (outputAs === 'webp') {
|
|
815
818
|
if (!modified) {
|
|
816
819
|
startMessage();
|
|
@@ -832,11 +835,10 @@ class Jimp extends Image {
|
|
|
832
835
|
for (let i = 0, length = gif2webp.length; i < length; ++i) {
|
|
833
836
|
const arg = gif2webp[i];
|
|
834
837
|
switch (arg) {
|
|
835
|
-
case '-h':
|
|
836
|
-
case '-version':
|
|
837
|
-
continue;
|
|
838
838
|
case '-o':
|
|
839
839
|
++i;
|
|
840
|
+
case '-h':
|
|
841
|
+
case '-version':
|
|
840
842
|
continue;
|
|
841
843
|
case '-q':
|
|
842
844
|
case '-m':
|
|
@@ -854,22 +856,22 @@ class Jimp extends Image {
|
|
|
854
856
|
}
|
|
855
857
|
args.push('-o', (0, util_1.normalizePath)(webp));
|
|
856
858
|
try {
|
|
857
|
-
child_process.execFile((0, util_1.
|
|
859
|
+
child_process.execFile(await (0, util_1.importBinary)('gif2webp', webp_path), args, { shell: true, signal: this.signal, ...execOptions(this.settings) }, (err, stdout) => {
|
|
858
860
|
if (!err) {
|
|
859
861
|
this.addLog(types_1.STATUS_TYPE.INFO, stdout);
|
|
860
862
|
finalize(webp);
|
|
861
863
|
}
|
|
862
864
|
else {
|
|
863
|
-
|
|
865
|
+
reject(err);
|
|
864
866
|
}
|
|
865
867
|
});
|
|
866
868
|
}
|
|
867
869
|
catch (err) {
|
|
868
|
-
if (this.checkPackage(err, 'gif2webp-bin
|
|
870
|
+
if (this.checkPackage(err, 'gif2webp-bin', 2048)) {
|
|
869
871
|
resolve();
|
|
870
872
|
}
|
|
871
873
|
else {
|
|
872
|
-
|
|
874
|
+
reject(err);
|
|
873
875
|
}
|
|
874
876
|
}
|
|
875
877
|
}
|
|
@@ -913,20 +915,20 @@ class Jimp extends Image {
|
|
|
913
915
|
}
|
|
914
916
|
GifUtil.write(output, frames, gif)
|
|
915
917
|
.then(() => {
|
|
916
|
-
transformWebP(output, true);
|
|
918
|
+
void transformWebP(output, true);
|
|
917
919
|
})
|
|
918
|
-
.catch(
|
|
920
|
+
.catch(reject);
|
|
919
921
|
})
|
|
920
|
-
.catch(
|
|
922
|
+
.catch(reject);
|
|
921
923
|
})
|
|
922
|
-
.catch(
|
|
924
|
+
.catch(reject);
|
|
923
925
|
}
|
|
924
926
|
catch (err) {
|
|
925
|
-
|
|
927
|
+
reject(err);
|
|
926
928
|
}
|
|
927
929
|
}
|
|
928
930
|
else {
|
|
929
|
-
transformWebP(localUri, false);
|
|
931
|
+
void transformWebP(localUri, false);
|
|
930
932
|
}
|
|
931
933
|
}
|
|
932
934
|
else {
|
|
@@ -983,10 +985,10 @@ class Jimp extends Image {
|
|
|
983
985
|
.then(() => {
|
|
984
986
|
finalize(output);
|
|
985
987
|
})
|
|
986
|
-
.catch(
|
|
988
|
+
.catch(reject);
|
|
987
989
|
}
|
|
988
990
|
catch (err) {
|
|
989
|
-
|
|
991
|
+
reject(err);
|
|
990
992
|
}
|
|
991
993
|
}
|
|
992
994
|
else {
|
|
@@ -1008,12 +1010,12 @@ class Jimp extends Image {
|
|
|
1008
1010
|
.then(() => {
|
|
1009
1011
|
finalize(output);
|
|
1010
1012
|
})
|
|
1011
|
-
.catch(
|
|
1013
|
+
.catch(reject);
|
|
1012
1014
|
})
|
|
1013
|
-
.catch(
|
|
1015
|
+
.catch(reject);
|
|
1014
1016
|
}
|
|
1015
1017
|
})
|
|
1016
|
-
.catch(
|
|
1018
|
+
.catch(reject);
|
|
1017
1019
|
}
|
|
1018
1020
|
else {
|
|
1019
1021
|
resolve();
|
|
@@ -1024,7 +1026,7 @@ class Jimp extends Image {
|
|
|
1024
1026
|
if (WEBPMUX_INIT) {
|
|
1025
1027
|
this.writeFail(["Unknown", 'node-webpmux'], err, { type: 2048, startTime });
|
|
1026
1028
|
}
|
|
1027
|
-
else if (this.checkPackage(err, 'node-webpmux
|
|
1029
|
+
else if (this.checkPackage(err, 'node-webpmux', 2048)) {
|
|
1028
1030
|
resolve();
|
|
1029
1031
|
return true;
|
|
1030
1032
|
}
|
|
@@ -1035,7 +1037,7 @@ class Jimp extends Image {
|
|
|
1035
1037
|
const { path: webp_path } = settings.webp ||= {};
|
|
1036
1038
|
const bmpFile = getTempPath(this, 'bmp');
|
|
1037
1039
|
try {
|
|
1038
|
-
child_process.execFile((0, util_1.
|
|
1040
|
+
child_process.execFile(await (0, util_1.importBinary)('dwebp', webp_path), [(0, util_1.normalizePath)(localUri), '-bmp', '-o', (0, util_1.normalizePath)(bmpFile)], { shell: true, signal: this.signal, ...execOptions(settings) }, err => {
|
|
1039
1041
|
if (!err) {
|
|
1040
1042
|
transformBuffer(bmpFile);
|
|
1041
1043
|
}
|
|
@@ -1043,7 +1045,7 @@ class Jimp extends Image {
|
|
|
1043
1045
|
removeFile(bmpFile);
|
|
1044
1046
|
void tryWebpMux().then(valid => {
|
|
1045
1047
|
if (!valid) {
|
|
1046
|
-
|
|
1048
|
+
reject(err);
|
|
1047
1049
|
}
|
|
1048
1050
|
});
|
|
1049
1051
|
}
|
|
@@ -1051,11 +1053,11 @@ class Jimp extends Image {
|
|
|
1051
1053
|
}
|
|
1052
1054
|
catch (err) {
|
|
1053
1055
|
if (!await tryWebpMux()) {
|
|
1054
|
-
if (this.checkPackage(err, 'dwebp-bin
|
|
1056
|
+
if (this.checkPackage(err, 'dwebp-bin', 2048)) {
|
|
1055
1057
|
resolve();
|
|
1056
1058
|
}
|
|
1057
1059
|
else {
|
|
1058
|
-
|
|
1060
|
+
reject(err);
|
|
1059
1061
|
}
|
|
1060
1062
|
}
|
|
1061
1063
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/jimp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Jimp V0 image constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
6
7
|
"publishConfig": {
|
|
7
8
|
"access": "public"
|
|
8
9
|
},
|
|
@@ -19,8 +20,8 @@
|
|
|
19
20
|
"license": "MIT",
|
|
20
21
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"@e-mc/image": "^0.
|
|
23
|
-
"@e-mc/types": "^0.
|
|
23
|
+
"@e-mc/image": "^0.12.0",
|
|
24
|
+
"@e-mc/types": "^0.12.0",
|
|
24
25
|
"bmp-js": "^0.1.0",
|
|
25
26
|
"gifwrap": "^0.10.1",
|
|
26
27
|
"jimp": "^0.22.12"
|
package/types/index.d.ts
CHANGED
|
@@ -19,8 +19,7 @@ export interface JimpSettings extends ImageSettings {
|
|
|
19
19
|
|
|
20
20
|
export type ResultCallback<T = unknown, U = void, V = unknown> = (err: V, result: T) => U;
|
|
21
21
|
|
|
22
|
-
export interface IJimpHandler<T extends IHost = IHost, U extends ImageModule = ImageModule> extends ImageHandler<jimp, T, IImage<T, U>> {
|
|
23
|
-
method(): Promise<void>;
|
|
22
|
+
export interface IJimpHandler<T extends IHost = IHost, U extends ImageModule = ImageModule> extends ImageHandler<jimp, T, IImage<T, U>, void, Promise<void>> {
|
|
24
23
|
quality(): void;
|
|
25
24
|
rotate(localFile?: string, callback?: ResultCallback<string>): Promise<this>;
|
|
26
25
|
background(value: number | [number, number, number, number]): void;
|
package/util.d.ts
CHANGED
|
@@ -2,7 +2,9 @@ declare namespace util {
|
|
|
2
2
|
function parseFormat(command: string, mimeType?: string, gif?: boolean): [string, string, string];
|
|
3
3
|
function renameExt(output: string, ext: string, replace?: boolean): string;
|
|
4
4
|
function normalizePath(value: string): string;
|
|
5
|
+
/** @deprecated */
|
|
5
6
|
function getWebP_bin(name: string, pathname: string | undefined): string;
|
|
7
|
+
function importBinary(name: string, pathname: string | undefined): Promise<string>;
|
|
6
8
|
function showInputType(value: string | undefined, outputType: string, finalAs: string): string;
|
|
7
9
|
function showOutputType(value: string | undefined, outputType: string, finalAs: string): string;
|
|
8
10
|
}
|
package/util.js
CHANGED
|
@@ -4,27 +4,27 @@ exports.parseFormat = parseFormat;
|
|
|
4
4
|
exports.renameExt = renameExt;
|
|
5
5
|
exports.normalizePath = normalizePath;
|
|
6
6
|
exports.getWebP_bin = getWebP_bin;
|
|
7
|
+
exports.importBinary = importBinary;
|
|
7
8
|
exports.showInputType = showInputType;
|
|
8
9
|
exports.showOutputType = showOutputType;
|
|
9
10
|
const path = require("node:path");
|
|
10
11
|
const fs = require("node:fs");
|
|
11
|
-
const jimp = require("jimp");
|
|
12
12
|
const types = require("@e-mc/types");
|
|
13
13
|
const Image = require("@e-mc/image");
|
|
14
14
|
exports.MIME_INPUT = new Set([
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
Image.MIME_PNG,
|
|
16
|
+
Image.MIME_JPEG,
|
|
17
|
+
Image.MIME_BMP,
|
|
18
|
+
Image.MIME_GIF,
|
|
19
|
+
Image.MIME_TIFF,
|
|
20
|
+
Image.MIME_WEBP
|
|
21
21
|
]);
|
|
22
22
|
exports.MIME_OUTPUT = new Set([
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
Image.MIME_PNG,
|
|
24
|
+
Image.MIME_JPEG,
|
|
25
|
+
Image.MIME_BMP,
|
|
26
|
+
Image.MIME_GIF,
|
|
27
|
+
Image.MIME_WEBP
|
|
28
28
|
]);
|
|
29
29
|
function parseFormat(command, mimeType, gif) {
|
|
30
30
|
command = command.toLowerCase();
|
|
@@ -38,21 +38,17 @@ function parseFormat(command, mimeType, gif) {
|
|
|
38
38
|
saveAs = 'jpg';
|
|
39
39
|
break;
|
|
40
40
|
case 'webp':
|
|
41
|
-
if (mimeType ===
|
|
42
|
-
mime =
|
|
41
|
+
if (mimeType === Image.MIME_JPEG) {
|
|
42
|
+
mime = Image.MIME_JPEG;
|
|
43
43
|
saveAs = 'jpg';
|
|
44
44
|
}
|
|
45
|
-
else if (gif && mimeType ===
|
|
46
|
-
mime =
|
|
45
|
+
else if (gif && mimeType === Image.MIME_GIF) {
|
|
46
|
+
mime = Image.MIME_GIF;
|
|
47
47
|
saveAs = 'gif';
|
|
48
48
|
}
|
|
49
|
-
else if (mimeType === jimp.MIME_PNG) {
|
|
50
|
-
mime = jimp.MIME_PNG;
|
|
51
|
-
saveAs = 'png';
|
|
52
|
-
}
|
|
53
49
|
else {
|
|
54
|
-
mime =
|
|
55
|
-
saveAs = '
|
|
50
|
+
mime = Image.MIME_PNG;
|
|
51
|
+
saveAs = 'png';
|
|
56
52
|
}
|
|
57
53
|
outputAs = 'webp';
|
|
58
54
|
break;
|
|
@@ -84,10 +80,18 @@ function getWebP_bin(name, pathname) {
|
|
|
84
80
|
if (pathname && fs.existsSync(pathname)) {
|
|
85
81
|
name += Image.PLATFORM_WIN32 ? '.exe' : '';
|
|
86
82
|
const bin = path.join(pathname, name);
|
|
87
|
-
return
|
|
83
|
+
return types.sanitizeCmd(fs.existsSync(bin) ? bin : path.join(pathname, 'bin', name));
|
|
88
84
|
}
|
|
89
85
|
return require(name + '-bin');
|
|
90
86
|
}
|
|
87
|
+
async function importBinary(name, pathname) {
|
|
88
|
+
if (pathname && fs.existsSync(pathname)) {
|
|
89
|
+
name += Image.PLATFORM_WIN32 ? '.exe' : '';
|
|
90
|
+
const bin = path.join(pathname, name);
|
|
91
|
+
return Image.sanitizeCmd(fs.existsSync(bin) ? bin : path.join(pathname, 'bin', name));
|
|
92
|
+
}
|
|
93
|
+
return types.importESM(name + '-bin', true);
|
|
94
|
+
}
|
|
91
95
|
function showInputType(value, outputType, outputAs) {
|
|
92
96
|
if (outputAs) {
|
|
93
97
|
outputType = 'image/' + outputAs;
|