@pi-r/jimp 0.3.6 → 0.3.7

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 +186 -154
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -30,13 +30,16 @@ const METHOD_ALIAS = {
30
30
  composite: 'cp',
31
31
  mask: 'ma',
32
32
  convolute: 'cl',
33
+ convolution: 'cl',
33
34
  flip: 'fl',
34
35
  mirror: 'mi',
35
36
  rotate: 'ro',
36
37
  brightness: 'br',
37
38
  contrast: 'cn',
38
39
  dither565: 'dt',
40
+ dither16: 'dt',
39
41
  greyscale: 'gr',
42
+ grayscale: 'gr',
40
43
  invert: 'in',
41
44
  normalize: 'no',
42
45
  fade: 'fa',
@@ -49,7 +52,19 @@ const METHOD_ALIAS = {
49
52
  sepia: 'se',
50
53
  pixelate: 'px',
51
54
  displace: 'dp',
52
- color: 'co'
55
+ color: 'co',
56
+ colour: 'co',
57
+ backgroundQuiet: 'bq',
58
+ circle: 'ci',
59
+ colorType: 'ce',
60
+ cropQuiet: 'cq',
61
+ deflateLevel: 'dl',
62
+ deflateStrategy: 'ds',
63
+ fishEye: 'fe',
64
+ filterType: 'ft',
65
+ rgba: 'rg',
66
+ shadow: 'sh',
67
+ threshold: 'th'
53
68
  };
54
69
  function getMethodName(value) {
55
70
  if (value.length === 2) {
@@ -70,6 +85,21 @@ function performCommand(host, instance, localUri, command, outputType, finalAs,
70
85
  return transformCommand(localUri, new JimpHandler(img, instance, host), command, outputType, finalAs, parent);
71
86
  });
72
87
  }
88
+ function execOptions(settings) {
89
+ var _a;
90
+ const exec = (_a = settings.jimp) === null || _a === void 0 ? void 0 : _a.exec;
91
+ let uid, gid;
92
+ if ((0, types_1.isPlainObject)(exec)) {
93
+ let { uid: u, gid: g } = exec;
94
+ if ((u = parseInt(u)) >= 0) {
95
+ uid = u;
96
+ }
97
+ if ((g = parseInt(g)) >= 0) {
98
+ gid = g;
99
+ }
100
+ }
101
+ return { uid, gid };
102
+ }
73
103
  async function transformCommand(localFile, handler, command, outputType, outputAs, parent) {
74
104
  if (command) {
75
105
  handler.instance.setCommand(command, outputAs);
@@ -77,14 +107,12 @@ async function transformCommand(localFile, handler, command, outputType, outputA
77
107
  await handler.method();
78
108
  handler.resize();
79
109
  handler.crop();
80
- if (outputType === jimp.MIME_JPEG) {
81
- if (!outputAs) {
82
- handler.quality();
83
- }
84
- }
85
- else {
110
+ if (outputType !== jimp.MIME_JPEG) {
86
111
  handler.opacity();
87
112
  }
113
+ else if (!outputAs) {
114
+ handler.quality();
115
+ }
88
116
  switch (handler.rotateCount) {
89
117
  case 0:
90
118
  return handler;
@@ -255,31 +283,32 @@ class JimpHandler {
255
283
  return;
256
284
  }
257
285
  const data = this.instance.methodData;
258
- if (data) {
259
- const handler = this.handler;
260
- for (const [name, args = []] of data) {
261
- try {
262
- const alias = getMethodName(name);
263
- if (!alias) {
264
- throw (0, types_1.errorValue)("Invalid method name", name);
265
- }
266
- if (alias === 'composite') {
267
- const [src, x, y, opts] = args;
268
- if ((0, types_1.isString)(src) && typeof x === 'number' && typeof y === 'number') {
269
- handler.composite(await jimp.read(src), x, y, opts);
270
- }
271
- else {
272
- throw (0, types_1.errorValue)("Invalid parameters", alias);
273
- }
286
+ if (!data) {
287
+ return;
288
+ }
289
+ const handler = this.handler;
290
+ for (const [name, args = []] of data) {
291
+ try {
292
+ const alias = getMethodName(name);
293
+ if (!alias) {
294
+ throw (0, types_1.errorValue)("Invalid method name", name);
295
+ }
296
+ if (alias === 'composite') {
297
+ const [src, x, y, opts] = args;
298
+ if ((0, types_1.isString)(src) && typeof x === 'number' && typeof y === 'number') {
299
+ handler.composite(await jimp.read(src), x, y, opts);
274
300
  }
275
301
  else {
276
- handler[alias](...args);
302
+ throw (0, types_1.errorValue)("Invalid parameters", alias);
277
303
  }
278
304
  }
279
- catch (err) {
280
- this.instance.writeFail(["Unknown", "jimp" + ': ' + name], err, 2048);
305
+ else {
306
+ handler[alias](...args);
281
307
  }
282
308
  }
309
+ catch (err) {
310
+ this.instance.writeFail(["Unknown", "jimp" + ': ' + name], err, 2048);
311
+ }
283
312
  }
284
313
  }
285
314
  resize() {
@@ -287,63 +316,64 @@ class JimpHandler {
287
316
  return;
288
317
  }
289
318
  const data = this.instance.resizeData;
290
- if (data) {
291
- const { width, height, color, algorithm, align, mode } = data;
292
- const handler = this.handler;
293
- if (!isNaN(color)) {
294
- handler.background(color);
295
- }
296
- let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR, flags = 0;
297
- switch (algorithm) {
298
- case 'bilinear':
299
- resizeMode = jimp.RESIZE_BILINEAR;
300
- break;
301
- case 'bicubic':
302
- resizeMode = jimp.RESIZE_BICUBIC;
303
- break;
304
- case 'hermite':
305
- resizeMode = jimp.RESIZE_HERMITE;
306
- break;
307
- case 'bezier':
308
- resizeMode = jimp.RESIZE_BEZIER;
309
- break;
310
- }
311
- switch (align[0]) {
312
- case 'left':
313
- flags |= jimp.HORIZONTAL_ALIGN_LEFT;
314
- break;
315
- case 'center':
316
- flags |= jimp.HORIZONTAL_ALIGN_CENTER;
317
- break;
318
- case 'right':
319
- flags |= jimp.HORIZONTAL_ALIGN_RIGHT;
320
- break;
321
- }
322
- switch (align[1]) {
323
- case 'top':
324
- flags |= jimp.VERTICAL_ALIGN_TOP;
325
- break;
326
- case 'middle':
327
- flags |= jimp.VERTICAL_ALIGN_MIDDLE;
328
- break;
329
- case 'bottom':
330
- flags |= jimp.VERTICAL_ALIGN_BOTTOM;
331
- break;
332
- }
333
- switch (mode) {
334
- case 'contain':
335
- handler.contain(width, height, flags);
336
- break;
337
- case 'cover':
338
- handler.cover(width, height, flags);
339
- break;
340
- case 'scale':
341
- handler.scaleToFit(width, height);
342
- break;
343
- default:
344
- handler.resize(width === Infinity ? jimp.AUTO : width, height === Infinity ? jimp.AUTO : height, resizeMode);
345
- break;
346
- }
319
+ if (!data) {
320
+ return;
321
+ }
322
+ const { width, height, color, algorithm, align, mode } = data;
323
+ const handler = this.handler;
324
+ if (!isNaN(color)) {
325
+ handler.background(color);
326
+ }
327
+ let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR, flags = 0;
328
+ switch (algorithm) {
329
+ case 'bilinear':
330
+ resizeMode = jimp.RESIZE_BILINEAR;
331
+ break;
332
+ case 'bicubic':
333
+ resizeMode = jimp.RESIZE_BICUBIC;
334
+ break;
335
+ case 'hermite':
336
+ resizeMode = jimp.RESIZE_HERMITE;
337
+ break;
338
+ case 'bezier':
339
+ resizeMode = jimp.RESIZE_BEZIER;
340
+ break;
341
+ }
342
+ switch (align[0]) {
343
+ case 'left':
344
+ flags |= jimp.HORIZONTAL_ALIGN_LEFT;
345
+ break;
346
+ case 'center':
347
+ flags |= jimp.HORIZONTAL_ALIGN_CENTER;
348
+ break;
349
+ case 'right':
350
+ flags |= jimp.HORIZONTAL_ALIGN_RIGHT;
351
+ break;
352
+ }
353
+ switch (align[1]) {
354
+ case 'top':
355
+ flags |= jimp.VERTICAL_ALIGN_TOP;
356
+ break;
357
+ case 'middle':
358
+ flags |= jimp.VERTICAL_ALIGN_MIDDLE;
359
+ break;
360
+ case 'bottom':
361
+ flags |= jimp.VERTICAL_ALIGN_BOTTOM;
362
+ break;
363
+ }
364
+ switch (mode) {
365
+ case 'contain':
366
+ handler.contain(width, height, flags);
367
+ break;
368
+ case 'cover':
369
+ handler.cover(width, height, flags);
370
+ break;
371
+ case 'scale':
372
+ handler.scaleToFit(width, height);
373
+ break;
374
+ default:
375
+ handler.resize(width === Infinity ? jimp.AUTO : width, height === Infinity ? jimp.AUTO : height, resizeMode);
376
+ break;
347
377
  }
348
378
  }
349
379
  background(value) {
@@ -353,13 +383,13 @@ class JimpHandler {
353
383
  this.handler.background(value);
354
384
  }
355
385
  finalize(output, callback) {
356
- var _a;
357
386
  if (this.aborted) {
358
387
  return;
359
388
  }
360
389
  const instance = this.instance;
361
390
  if (instance.outputAs === 'webp' && path.extname(output).toLowerCase() !== '.webp') {
362
- const webp = (_a = instance.settings).webp || (_a.webp = {});
391
+ const settings = instance.settings;
392
+ const webp = settings.webp || (settings.webp = {});
363
393
  const data = instance.qualityData;
364
394
  const replace = instance.getCommand().indexOf('@') !== -1;
365
395
  const filename = (0, util_1.renameExt)(output, 'webp', replace);
@@ -406,7 +436,7 @@ class JimpHandler {
406
436
  }
407
437
  args.push('-o', (0, util_1.normalizePath)(filename));
408
438
  try {
409
- child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal }, err => {
439
+ child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
410
440
  if (err) {
411
441
  this.instance.writeFail(["Unable to convert file", path.basename(filename)], err, 2048);
412
442
  }
@@ -545,67 +575,67 @@ class Jimp extends Image {
545
575
  static async transform(file, command, options = {}) {
546
576
  const [outputType, saveAs, finalAs] = (0, util_1.parseFormat)(command = command.trim(), options.mimeType);
547
577
  const empty = () => options.tempFile ? '' : null;
548
- if (outputType) {
549
- const instance = new Jimp(options.module);
550
- let buffer = null;
551
- if (Buffer.isBuffer(file)) {
552
- const tempDir = TEMP_DIR || instance.getTempDir();
553
- if (!this.createDir(tempDir)) {
554
- return empty();
555
- }
556
- try {
557
- const { ext } = await this.resolveMime(file) || { ext: 'unknown' };
558
- buffer = file;
559
- fs.writeFileSync(file = path.join(tempDir, (0, types_1.generateUUID)() + '.' + ext), buffer);
560
- }
561
- catch {
562
- return empty();
563
- }
564
- options.cache = false;
565
- }
566
- const filename = path.basename(file);
567
- const broadcastId = options.broadcastId;
568
- if (broadcastId) {
569
- if ((0, types_1.isPlainObject)(broadcastId)) {
570
- instance.broadcastId = broadcastId.value;
571
- if (broadcastId.stripAnsi === false) {
572
- instance.supports('stripAnsi', false);
573
- }
574
- }
575
- else {
576
- instance.broadcastId = broadcastId;
577
- }
578
+ if (!outputType) {
579
+ return empty();
580
+ }
581
+ const instance = new Jimp(options.module);
582
+ let buffer = null;
583
+ if (Buffer.isBuffer(file)) {
584
+ const tempDir = TEMP_DIR || instance.getTempDir();
585
+ if (!this.createDir(tempDir)) {
586
+ return empty();
578
587
  }
579
- const writeMessage = (failed, cTimeMs) => {
580
- if (cTimeMs || options.startTime) {
581
- formatMessage.call(instance, filename + (0, util_1.showOutputType)(options.mimeType, outputType, finalAs), options.startTime, failed, cTimeMs);
582
- }
583
- };
584
- let tempKey, tempFile;
585
- if (options.cache) {
586
- let ctimeMs;
587
- [buffer, tempFile, ctimeMs] = getImageCache.call(instance, tempKey = file + command + (options.mimeType || ''));
588
- if (buffer) {
589
- writeMessage(false, ctimeMs);
590
- return Promise.resolve(buffer);
591
- }
588
+ try {
589
+ const { ext } = await this.resolveMime(file) || { ext: 'unknown' };
590
+ buffer = file;
591
+ fs.writeFileSync(file = path.join(tempDir, (0, types_1.generateUUID)() + '.' + ext), buffer);
592
592
  }
593
- instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp", ["Transforming image...", filename], command);
594
- Image.initCpuUsage(instance);
595
- return performCommand(null, instance, file, command, outputType, finalAs)
596
- .then(async (handler) => {
597
- const result = await handler.getBuffer(options.tempFile, saveAs);
598
- instance.flushLog();
599
- writeMessage(!result || instance.errors.length > 0);
600
- if (result && tempKey && tempFile) {
601
- setImageCache.call(instance, tempKey, tempFile, result, file);
602
- }
603
- return result;
604
- })
605
- .catch(() => empty())
606
- .finally(() => buffer && !options.cache && removeFile(file));
593
+ catch {
594
+ return empty();
595
+ }
596
+ options.cache = false;
607
597
  }
608
- return empty();
598
+ const filename = path.basename(file);
599
+ const broadcastId = options.broadcastId;
600
+ if (broadcastId) {
601
+ if ((0, types_1.isPlainObject)(broadcastId)) {
602
+ instance.broadcastId = broadcastId.value;
603
+ if (broadcastId.stripAnsi === false) {
604
+ instance.supports('stripAnsi', false);
605
+ }
606
+ }
607
+ else {
608
+ instance.broadcastId = broadcastId;
609
+ }
610
+ }
611
+ const writeMessage = (failed, cTimeMs) => {
612
+ if (cTimeMs || options.startTime) {
613
+ formatMessage.call(instance, filename + (0, util_1.showOutputType)(options.mimeType, outputType, finalAs), options.startTime, failed, cTimeMs);
614
+ }
615
+ };
616
+ let tempKey, tempFile;
617
+ if (options.cache) {
618
+ let ctimeMs;
619
+ [buffer, tempFile, ctimeMs] = getImageCache.call(instance, tempKey = file + command + (options.mimeType || ''));
620
+ if (buffer) {
621
+ writeMessage(false, ctimeMs);
622
+ return Promise.resolve(buffer);
623
+ }
624
+ }
625
+ instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp", ["Transforming image...", filename], command);
626
+ Image.initCpuUsage(instance);
627
+ return performCommand(null, instance, file, command, outputType, finalAs)
628
+ .then(async (handler) => {
629
+ const result = await handler.getBuffer(options.tempFile, saveAs);
630
+ instance.flushLog();
631
+ writeMessage(!result || instance.errors.length > 0);
632
+ if (result && tempKey && tempFile) {
633
+ setImageCache.call(instance, tempKey, tempFile, result, file);
634
+ }
635
+ return result;
636
+ })
637
+ .catch(() => empty())
638
+ .finally(() => buffer && !options.cache && removeFile(file));
609
639
  }
610
640
  parseRotate(value) {
611
641
  var _a;
@@ -624,7 +654,6 @@ class Jimp extends Image {
624
654
  }
625
655
  return new Promise(async (resolve, reject) => {
626
656
  var _a;
627
- var _b;
628
657
  const { host, file } = data;
629
658
  const localUri = host.getLocalUri(data);
630
659
  const mimeType = host.getMimeType(data);
@@ -654,8 +683,9 @@ class Jimp extends Image {
654
683
  const startTime = process.hrtime();
655
684
  const success = (result, ctimeMs) => {
656
685
  const filename = path.basename(result);
657
- if (file.document) {
658
- host.writeImage(file.document, data.getObject({ command, output: result }));
686
+ const document = file.document;
687
+ if (document) {
688
+ host.writeImage(document, data.getObject({ command, output: result }));
659
689
  }
660
690
  if (host.getLocalUri(data) !== result) {
661
691
  if (command.indexOf('%') !== -1) {
@@ -766,11 +796,12 @@ class Jimp extends Image {
766
796
  const args = [(0, util_1.normalizePath)(target)];
767
797
  const quality = cmd.quality;
768
798
  if (quality) {
769
- if (!isNaN(quality.value)) {
770
- args.push('-q', quality.value.toString());
799
+ const { value, method } = quality;
800
+ if (!isNaN(value)) {
801
+ args.push('-q', value.toString());
771
802
  }
772
- if (!isNaN(quality.method)) {
773
- args.push('-m', quality.method.toString());
803
+ if (!isNaN(method)) {
804
+ args.push('-m', method.toString());
774
805
  }
775
806
  }
776
807
  if (Array.isArray(gif2webp)) {
@@ -799,7 +830,7 @@ class Jimp extends Image {
799
830
  }
800
831
  args.push('-o', (0, util_1.normalizePath)(webp));
801
832
  try {
802
- child_process.execFile((0, util_1.getWebP_bin)('gif2webp', webp_path), args, { shell: true, signal: this.signal }, (err, stdout) => {
833
+ child_process.execFile((0, util_1.getWebP_bin)('gif2webp', webp_path), args, { shell: true, signal: this.signal, ...execOptions(this.settings) }, (err, stdout) => {
803
834
  if (!err) {
804
835
  this.addLog(types_1.STATUS_TYPE.INFO, stdout);
805
836
  finalize(webp);
@@ -944,10 +975,11 @@ class Jimp extends Image {
944
975
  }
945
976
  return false;
946
977
  };
947
- const { path: webp_path } = (_b = this.settings).webp || (_b.webp = {});
978
+ const settings = this.settings;
979
+ const { path: webp_path } = settings.webp || (settings.webp = {});
948
980
  const bmpFile = getTempPath.call(this, 'bmp');
949
981
  try {
950
- 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 => {
982
+ 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 => {
951
983
  if (!err) {
952
984
  transformBuffer(bmpFile);
953
985
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/jimp",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "description": "Jimp image constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "publishConfig": {