b2b-platform-utils 1.1.40 → 1.1.41

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/optimizeMedia.js +42 -1
  2. package/package.json +1 -1
package/optimizeMedia.js CHANGED
@@ -45,4 +45,45 @@ async function saveGameImageBufferWEBP(fileDataBuffer, quality = 80, maxWidth =
45
45
  .toBuffer();
46
46
  }
47
47
 
48
- module.exports = { optimizeImageBufferWEBP, saveGameImageBufferWEBP };
48
+ /**
49
+ * Buffer to WebP for game images: auto-rotate; optional resize when `width` and/or `height` is set (Sharp options).
50
+ * @param {Buffer} fileDataBuffer
51
+ * @param {object} [options]
52
+ * @returns {Promise<Buffer>}
53
+ */
54
+ async function saveGameImageBufferWEBPWithOptions(fileDataBuffer, options = {}) {
55
+ const {
56
+ quality = 80,
57
+ width,
58
+ height,
59
+ fit = 'inside',
60
+ withoutEnlargement = true,
61
+ effort = 5,
62
+ smartSubsample = true
63
+ } = options;
64
+
65
+ let pipeline = sharp(fileDataBuffer).rotate();
66
+
67
+ if (width != null || height != null) {
68
+ pipeline = pipeline.resize({
69
+ width,
70
+ height,
71
+ fit,
72
+ withoutEnlargement
73
+ });
74
+ }
75
+
76
+ return pipeline
77
+ .webp({
78
+ quality,
79
+ effort,
80
+ smartSubsample
81
+ })
82
+ .toBuffer();
83
+ }
84
+
85
+ module.exports = {
86
+ optimizeImageBufferWEBP,
87
+ saveGameImageBufferWEBP,
88
+ saveGameImageBufferWEBPWithOptions
89
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "b2b-platform-utils",
3
- "version": "1.1.40",
3
+ "version": "1.1.41",
4
4
  "description": "Shared utilities for Node.js microservices: errors map, local cache, logger, numbers, dates, filesystem, media optimization, paginator, slugger, crypto wrapper, sanitize HTML, sorting.",
5
5
  "type": "commonjs",
6
6
  "license": "KingSizer",