@pi-r/jimp 0.2.2 → 0.3.1

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.
Files changed (2) hide show
  1. package/index.js +187 -156
  2. package/package.json +5 -5
package/index.js CHANGED
@@ -24,13 +24,16 @@ const METHOD_ALIAS = {
24
24
  composite: 'cp',
25
25
  mask: 'ma',
26
26
  convolute: 'cl',
27
+ convolution: 'cl',
27
28
  flip: 'fl',
28
29
  mirror: 'mi',
29
30
  rotate: 'ro',
30
31
  brightness: 'br',
31
32
  contrast: 'cn',
32
33
  dither565: 'dt',
34
+ dither16: 'dt',
33
35
  greyscale: 'gr',
36
+ grayscale: 'gr',
34
37
  invert: 'in',
35
38
  normalize: 'no',
36
39
  fade: 'fa',
@@ -43,7 +46,19 @@ const METHOD_ALIAS = {
43
46
  sepia: 'se',
44
47
  pixelate: 'px',
45
48
  displace: 'dp',
46
- color: 'co'
49
+ color: 'co',
50
+ colour: 'co',
51
+ backgroundQuiet: 'bq',
52
+ circle: 'ci',
53
+ colorType: 'ce',
54
+ cropQuiet: 'cq',
55
+ deflateLevel: 'dl',
56
+ deflateStrategy: 'ds',
57
+ fishEye: 'fe',
58
+ filterType: 'ft',
59
+ rgba: 'rg',
60
+ shadow: 'sh',
61
+ threshold: 'th'
47
62
  };
48
63
  function getMethodName(value) {
49
64
  const name = value.toLowerCase();
@@ -64,6 +79,20 @@ function performCommand(host, instance, localUri, command, outputType, finalAs,
64
79
  return transformCommand(localUri, new JimpHandler(img, instance, host), command, outputType, finalAs, parent);
65
80
  });
66
81
  }
82
+ function execOptions(settings) {
83
+ const exec = settings.jimp?.exec;
84
+ let uid, gid;
85
+ if ((0, types_1.isPlainObject)(exec)) {
86
+ let { uid: u, gid: g } = exec;
87
+ if ((u = parseInt(u)) >= 0) {
88
+ uid = u;
89
+ }
90
+ if ((g = parseInt(g)) >= 0) {
91
+ gid = g;
92
+ }
93
+ }
94
+ return { uid, gid };
95
+ }
67
96
  async function transformCommand(localFile, handler, command, outputType, outputAs, parent) {
68
97
  if (command) {
69
98
  handler.instance.setCommand(command, outputAs);
@@ -71,14 +100,12 @@ async function transformCommand(localFile, handler, command, outputType, outputA
71
100
  await handler.method();
72
101
  handler.resize();
73
102
  handler.crop();
74
- if (outputType === jimp.MIME_JPEG) {
75
- if (!outputAs) {
76
- handler.quality();
77
- }
78
- }
79
- else {
103
+ if (outputType !== jimp.MIME_JPEG) {
80
104
  handler.opacity();
81
105
  }
106
+ else if (!outputAs) {
107
+ handler.quality();
108
+ }
82
109
  switch (handler.rotateCount) {
83
110
  case 0:
84
111
  return handler;
@@ -86,9 +113,9 @@ async function transformCommand(localFile, handler, command, outputType, outputA
86
113
  return handler.rotate();
87
114
  default:
88
115
  return handler.rotate(localFile, (err, result) => {
89
- if (!err && handler.host) {
116
+ if (!err) {
90
117
  try {
91
- handler.host.add(result, parent);
118
+ handler.host?.add(result, parent);
92
119
  }
93
120
  catch {
94
121
  }
@@ -248,31 +275,32 @@ class JimpHandler {
248
275
  return;
249
276
  }
250
277
  const data = this.instance.methodData;
251
- if (data) {
252
- const handler = this.handler;
253
- for (const [name, args = []] of data) {
254
- try {
255
- const alias = getMethodName(name);
256
- if (!alias) {
257
- throw (0, types_1.errorValue)("Invalid method name" /* ERR_IMAGE.METHOD_NAME */, name);
258
- }
259
- if (alias === 'composite') {
260
- const [src, x, y, opts] = args;
261
- if ((0, types_1.isString)(src) && typeof x === 'number' && typeof y === 'number') {
262
- handler.composite(await jimp.read(src), x, y, opts);
263
- }
264
- else {
265
- throw (0, types_1.errorValue)("Invalid parameters" /* ERR_MESSAGE.PARAMETERS */, alias);
266
- }
278
+ if (!data) {
279
+ return;
280
+ }
281
+ const handler = this.handler;
282
+ for (const [name, args = []] of data) {
283
+ try {
284
+ const alias = getMethodName(name);
285
+ if (!alias) {
286
+ throw (0, types_1.errorValue)("Invalid method name" /* ERR_IMAGE.METHOD_NAME */, name);
287
+ }
288
+ if (alias === 'composite') {
289
+ const [src, x, y, opts] = args;
290
+ if ((0, types_1.isString)(src) && typeof x === 'number' && typeof y === 'number') {
291
+ handler.composite(await jimp.read(src), x, y, opts);
267
292
  }
268
293
  else {
269
- handler[alias](...args);
294
+ throw (0, types_1.errorValue)("Invalid parameters" /* ERR_MESSAGE.PARAMETERS */, alias);
270
295
  }
271
296
  }
272
- catch (err) {
273
- this.instance.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, "jimp" /* STRINGS.MODULE_NAME */ + ': ' + name], err, 2048 /* LOG_TYPE.IMAGE */);
297
+ else {
298
+ handler[alias](...args);
274
299
  }
275
300
  }
301
+ catch (err) {
302
+ this.instance.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, "jimp" /* STRINGS.MODULE_NAME */ + ': ' + name], err, 2048 /* LOG_TYPE.IMAGE */);
303
+ }
276
304
  }
277
305
  }
278
306
  resize() {
@@ -280,63 +308,64 @@ class JimpHandler {
280
308
  return;
281
309
  }
282
310
  const data = this.instance.resizeData;
283
- if (data) {
284
- const { width, height, color, algorithm, align, mode } = data;
285
- const handler = this.handler;
286
- if (!isNaN(color)) {
287
- handler.background(color);
288
- }
289
- let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR, flags = 0;
290
- switch (algorithm) {
291
- case 'bilinear':
292
- resizeMode = jimp.RESIZE_BILINEAR;
293
- break;
294
- case 'bicubic':
295
- resizeMode = jimp.RESIZE_BICUBIC;
296
- break;
297
- case 'hermite':
298
- resizeMode = jimp.RESIZE_HERMITE;
299
- break;
300
- case 'bezier':
301
- resizeMode = jimp.RESIZE_BEZIER;
302
- break;
303
- }
304
- switch (align[0]) {
305
- case 'left':
306
- flags |= jimp.HORIZONTAL_ALIGN_LEFT;
307
- break;
308
- case 'center':
309
- flags |= jimp.HORIZONTAL_ALIGN_CENTER;
310
- break;
311
- case 'right':
312
- flags |= jimp.HORIZONTAL_ALIGN_RIGHT;
313
- break;
314
- }
315
- switch (align[1]) {
316
- case 'top':
317
- flags |= jimp.VERTICAL_ALIGN_TOP;
318
- break;
319
- case 'middle':
320
- flags |= jimp.VERTICAL_ALIGN_MIDDLE;
321
- break;
322
- case 'bottom':
323
- flags |= jimp.VERTICAL_ALIGN_BOTTOM;
324
- break;
325
- }
326
- switch (mode) {
327
- case 'contain':
328
- handler.contain(width, height, flags);
329
- break;
330
- case 'cover':
331
- handler.cover(width, height, flags);
332
- break;
333
- case 'scale':
334
- handler.scaleToFit(width, height);
335
- break;
336
- default:
337
- handler.resize(width === Infinity ? jimp.AUTO : width, height === Infinity ? jimp.AUTO : height, resizeMode);
338
- break;
339
- }
311
+ if (!data) {
312
+ return;
313
+ }
314
+ const { width, height, color, algorithm, align, mode } = data;
315
+ const handler = this.handler;
316
+ if (!isNaN(color)) {
317
+ handler.background(color);
318
+ }
319
+ let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR, flags = 0;
320
+ switch (algorithm) {
321
+ case 'bilinear':
322
+ resizeMode = jimp.RESIZE_BILINEAR;
323
+ break;
324
+ case 'bicubic':
325
+ resizeMode = jimp.RESIZE_BICUBIC;
326
+ break;
327
+ case 'hermite':
328
+ resizeMode = jimp.RESIZE_HERMITE;
329
+ break;
330
+ case 'bezier':
331
+ resizeMode = jimp.RESIZE_BEZIER;
332
+ break;
333
+ }
334
+ switch (align[0]) {
335
+ case 'left':
336
+ flags |= jimp.HORIZONTAL_ALIGN_LEFT;
337
+ break;
338
+ case 'center':
339
+ flags |= jimp.HORIZONTAL_ALIGN_CENTER;
340
+ break;
341
+ case 'right':
342
+ flags |= jimp.HORIZONTAL_ALIGN_RIGHT;
343
+ break;
344
+ }
345
+ switch (align[1]) {
346
+ case 'top':
347
+ flags |= jimp.VERTICAL_ALIGN_TOP;
348
+ break;
349
+ case 'middle':
350
+ flags |= jimp.VERTICAL_ALIGN_MIDDLE;
351
+ break;
352
+ case 'bottom':
353
+ flags |= jimp.VERTICAL_ALIGN_BOTTOM;
354
+ break;
355
+ }
356
+ switch (mode) {
357
+ case 'contain':
358
+ handler.contain(width, height, flags);
359
+ break;
360
+ case 'cover':
361
+ handler.cover(width, height, flags);
362
+ break;
363
+ case 'scale':
364
+ handler.scaleToFit(width, height);
365
+ break;
366
+ default:
367
+ handler.resize(width === Infinity ? jimp.AUTO : width, height === Infinity ? jimp.AUTO : height, resizeMode);
368
+ break;
340
369
  }
341
370
  }
342
371
  background(value) {
@@ -346,13 +375,13 @@ class JimpHandler {
346
375
  this.handler.background(value);
347
376
  }
348
377
  finalize(output, callback) {
349
- var _a;
350
378
  if (this.aborted) {
351
379
  return;
352
380
  }
353
381
  const instance = this.instance;
354
382
  if (instance.outputAs === 'webp' && path.extname(output).toLowerCase() !== '.webp') {
355
- const webp = (_a = instance.settings).webp || (_a.webp = {});
383
+ const settings = instance.settings;
384
+ const webp = settings.webp || (settings.webp = {});
356
385
  const data = instance.qualityData;
357
386
  const replace = instance.getCommand().indexOf('@') !== -1;
358
387
  const filename = (0, util_1.renameExt)(output, 'webp', replace);
@@ -399,7 +428,7 @@ class JimpHandler {
399
428
  }
400
429
  args.push('-o', (0, util_1.normalizePath)(filename));
401
430
  try {
402
- child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal }, err => {
431
+ child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
403
432
  if (err) {
404
433
  this.instance.writeFail(["Unable to convert file" /* ERR_MESSAGE.CONVERT_FILE */, path.basename(filename)], err, 2048 /* LOG_TYPE.IMAGE */);
405
434
  }
@@ -537,67 +566,67 @@ class Jimp extends Image {
537
566
  static async transform(file, command, options = {}) {
538
567
  const [outputType, saveAs, finalAs] = (0, util_1.parseFormat)(command = command.trim(), options.mimeType);
539
568
  const empty = () => options.tempFile ? '' : null;
540
- if (outputType) {
541
- const instance = new Jimp(options.module);
542
- let buffer = null;
543
- if (Buffer.isBuffer(file)) {
544
- const tempDir = TEMP_DIR || instance.getTempDir();
545
- if (!this.createDir(tempDir)) {
546
- return empty();
547
- }
548
- try {
549
- const { ext } = await this.resolveMime(file) || { ext: 'unknown' };
550
- buffer = file;
551
- fs.writeFileSync(file = path.join(tempDir, (0, types_1.generateUUID)() + '.' + ext), buffer);
552
- }
553
- catch {
554
- return empty();
555
- }
556
- options.cache = false;
557
- }
558
- const filename = path.basename(file);
559
- const broadcastId = options.broadcastId;
560
- if (broadcastId) {
561
- if ((0, types_1.isPlainObject)(broadcastId)) {
562
- instance.broadcastId = broadcastId.value;
563
- if (broadcastId.stripAnsi === false) {
564
- instance.supports('stripAnsi', false);
565
- }
566
- }
567
- else {
568
- instance.broadcastId = broadcastId;
569
- }
569
+ if (!outputType) {
570
+ return empty();
571
+ }
572
+ const instance = new Jimp(options.module);
573
+ let buffer = null;
574
+ if (Buffer.isBuffer(file)) {
575
+ const tempDir = TEMP_DIR || instance.getTempDir();
576
+ if (!this.createDir(tempDir)) {
577
+ return empty();
570
578
  }
571
- const writeMessage = (failed, cTimeMs) => {
572
- if (cTimeMs || options.startTime) {
573
- formatMessage.call(instance, filename + (0, util_1.showOutputType)(options.mimeType, outputType, finalAs), options.startTime, failed, cTimeMs);
574
- }
575
- };
576
- let tempKey, tempFile;
577
- if (options.cache) {
578
- let ctimeMs;
579
- [buffer, tempFile, ctimeMs] = getImageCache.call(instance, tempKey = file + command + (options.mimeType || ''));
580
- if (buffer) {
581
- writeMessage(false, ctimeMs);
582
- return Promise.resolve(buffer);
583
- }
579
+ try {
580
+ const { ext } = await this.resolveMime(file) || { ext: 'unknown' };
581
+ buffer = file;
582
+ fs.writeFileSync(file = path.join(tempDir, (0, types_1.generateUUID)() + '.' + ext), buffer);
584
583
  }
585
- instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp" /* STRINGS.MODULE_NAME */, ["Transforming image..." /* STRINGS.TRANSFORM */, filename], command);
586
- Image.initCpuUsage(instance);
587
- return performCommand(null, instance, file, command, outputType, finalAs)
588
- .then(async (handler) => {
589
- const result = await handler.getBuffer(options.tempFile, saveAs);
590
- instance.flushLog();
591
- writeMessage(!result || instance.errors.length > 0);
592
- if (result && tempKey && tempFile) {
593
- setImageCache.call(instance, tempKey, tempFile, result, file);
594
- }
595
- return result;
596
- })
597
- .catch(() => empty())
598
- .finally(() => buffer && !options.cache && removeFile(file));
584
+ catch {
585
+ return empty();
586
+ }
587
+ options.cache = false;
599
588
  }
600
- return empty();
589
+ const filename = path.basename(file);
590
+ const broadcastId = options.broadcastId;
591
+ if (broadcastId) {
592
+ if ((0, types_1.isPlainObject)(broadcastId)) {
593
+ instance.broadcastId = broadcastId.value;
594
+ if (broadcastId.stripAnsi === false) {
595
+ instance.supports('stripAnsi', false);
596
+ }
597
+ }
598
+ else {
599
+ instance.broadcastId = broadcastId;
600
+ }
601
+ }
602
+ const writeMessage = (failed, cTimeMs) => {
603
+ if (cTimeMs || options.startTime) {
604
+ formatMessage.call(instance, filename + (0, util_1.showOutputType)(options.mimeType, outputType, finalAs), options.startTime, failed, cTimeMs);
605
+ }
606
+ };
607
+ let tempKey, tempFile;
608
+ if (options.cache) {
609
+ let ctimeMs;
610
+ [buffer, tempFile, ctimeMs] = getImageCache.call(instance, tempKey = file + command + (options.mimeType || ''));
611
+ if (buffer) {
612
+ writeMessage(false, ctimeMs);
613
+ return Promise.resolve(buffer);
614
+ }
615
+ }
616
+ instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp" /* STRINGS.MODULE_NAME */, ["Transforming image..." /* STRINGS.TRANSFORM */, filename], command);
617
+ Image.initCpuUsage(instance);
618
+ return performCommand(null, instance, file, command, outputType, finalAs)
619
+ .then(async (handler) => {
620
+ const result = await handler.getBuffer(options.tempFile, saveAs);
621
+ instance.flushLog();
622
+ writeMessage(!result || instance.errors.length > 0);
623
+ if (result && tempKey && tempFile) {
624
+ setImageCache.call(instance, tempKey, tempFile, result, file);
625
+ }
626
+ return result;
627
+ })
628
+ .catch(() => empty())
629
+ .finally(() => buffer && !options.cache && removeFile(file));
601
630
  }
602
631
  parseRotate(value) {
603
632
  const data = super.parseRotate(value);
@@ -614,7 +643,6 @@ class Jimp extends Image {
614
643
  return Promise.reject((0, types_1.createAbortError)());
615
644
  }
616
645
  return new Promise(async (resolve, reject) => {
617
- var _a;
618
646
  const { host, file } = data;
619
647
  const localUri = host.getLocalUri(data);
620
648
  const mimeType = host.getMimeType(data);
@@ -644,8 +672,9 @@ class Jimp extends Image {
644
672
  const startTime = process.hrtime();
645
673
  const success = (result, ctimeMs) => {
646
674
  const filename = path.basename(result);
647
- if (file.document) {
648
- host.writeImage(file.document, data.getObject({ command, output: result }));
675
+ const document = file.document;
676
+ if (document) {
677
+ host.writeImage(document, data.getObject({ command, output: result }));
649
678
  }
650
679
  if (host.getLocalUri(data) !== result) {
651
680
  if (command.indexOf('%') !== -1) {
@@ -756,11 +785,12 @@ class Jimp extends Image {
756
785
  const args = [(0, util_1.normalizePath)(target)];
757
786
  const quality = cmd.quality;
758
787
  if (quality) {
759
- if (!isNaN(quality.value)) {
760
- args.push('-q', quality.value.toString());
788
+ const { value, method } = quality;
789
+ if (!isNaN(value)) {
790
+ args.push('-q', value.toString());
761
791
  }
762
- if (!isNaN(quality.method)) {
763
- args.push('-m', quality.method.toString());
792
+ if (!isNaN(method)) {
793
+ args.push('-m', method.toString());
764
794
  }
765
795
  }
766
796
  if (Array.isArray(gif2webp)) {
@@ -789,7 +819,7 @@ class Jimp extends Image {
789
819
  }
790
820
  args.push('-o', (0, util_1.normalizePath)(webp));
791
821
  try {
792
- child_process.execFile((0, util_1.getWebP_bin)('gif2webp', webp_path), args, { shell: true, signal: this.signal }, (err, stdout) => {
822
+ child_process.execFile((0, util_1.getWebP_bin)('gif2webp', webp_path), args, { shell: true, signal: this.signal, ...execOptions(this.settings) }, (err, stdout) => {
793
823
  if (!err) {
794
824
  this.addLog(types_1.STATUS_TYPE.INFO, stdout);
795
825
  finalize(webp);
@@ -936,10 +966,11 @@ class Jimp extends Image {
936
966
  }
937
967
  return false;
938
968
  };
939
- const { path: webp_path } = (_a = this.settings).webp || (_a.webp = {});
969
+ const settings = this.settings;
970
+ const { path: webp_path } = settings.webp || (settings.webp = {});
940
971
  const bmpFile = getTempPath.call(this, 'bmp');
941
972
  try {
942
- child_process.execFile((0, util_1.getWebP_bin)('dwebp', webp_path), [(0, util_1.normalizePath)(localUri), '-bmp', '-o', (0, util_1.normalizePath)(bmpFile)], { shell: true, signal: this.signal }, err => {
973
+ child_process.execFile((0, util_1.getWebP_bin)('dwebp', webp_path), [(0, util_1.normalizePath)(localUri), '-bmp', '-o', (0, util_1.normalizePath)(bmpFile)], { shell: true, signal: this.signal, ...execOptions(settings) }, err => {
943
974
  if (!err) {
944
975
  transformBuffer(bmpFile);
945
976
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/jimp",
3
- "version": "0.2.2",
3
+ "version": "0.3.1",
4
4
  "description": "Jimp image constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -20,10 +20,10 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/image": "^0.5.3",
24
- "@e-mc/types": "^0.5.3",
23
+ "@e-mc/image": "^0.6.0",
24
+ "@e-mc/types": "^0.6.0",
25
25
  "bmp-js": "^0.1.0",
26
- "gifwrap": "^0.9.4",
27
- "jimp": "^0.22.8"
26
+ "gifwrap": "^0.10.1",
27
+ "jimp": "^0.22.10"
28
28
  }
29
29
  }