@shibam/sticker-maker 1.2.6 → 1.2.8

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 CHANGED
@@ -98,13 +98,10 @@ export default class Sticker {
98
98
  }
99
99
 
100
100
  async build() {
101
- // Process input to buffer
102
101
  const inputBuffer = await this.processImage(this.image);
103
102
 
104
- // Convert to WebP using imported converter
105
103
  const webpBuffer = await this.converter.convertToWebp(inputBuffer);
106
104
 
107
- // Add metadata using imported Exif handler
108
105
  const exif = new Exif({
109
106
  id: this.id,
110
107
  pack: this.pack,
@@ -120,11 +117,11 @@ export default class Sticker {
120
117
  return await this.build();
121
118
  }
122
119
 
123
- async metadata() {
124
- if (!this.buffer) {
125
- await this.build();
126
- }
127
-
128
- return await extractMetadata(this.buffer);
120
+ async metadata() {
121
+ if (!this.buffer) {
122
+ await this.build();
129
123
  }
124
+
125
+ return await extractMetadata(this.buffer);
126
+ }
130
127
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shibam/sticker-maker",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "main": "index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -11,10 +11,8 @@
11
11
  "license": "ISC",
12
12
  "description": "",
13
13
  "dependencies": {
14
- "cwebp": "^3.1.0",
15
14
  "file-type": "^20.0.0",
16
- "jimp": "^1.6.0",
17
15
  "node-webpmux": "^3.2.0",
18
- "webp-converter": "^2.3.3"
16
+ "sharp": "^0.33.5"
19
17
  }
20
18
  }
package/utils/convert.js CHANGED
@@ -4,15 +4,15 @@ import { promises as fs } from "fs";
4
4
  import { tmpdir } from "os";
5
5
  import { join } from "path";
6
6
  import { fileTypeFromBuffer } from "file-type";
7
-
7
+ import sharp from "sharp";
8
8
 
9
9
  const exec = promisify(execCallback);
10
10
 
11
11
  export default class MediaConverter {
12
12
  constructor(options = {}) {
13
- this.fps = options.fps || 12;
14
- this.quality = options.quality || 80;
15
- this.width = options.width || 512;
13
+ this.fps = 12;
14
+ this.quality = options.quality ?? 10;
15
+ this.width = options.width ?? 320;
16
16
  this.mime = null;
17
17
  }
18
18
 
@@ -24,11 +24,11 @@ export default class MediaConverter {
24
24
 
25
25
  if (this.mime.startsWith("video/")) {
26
26
  const gif = await this.videoToGif(buffer);
27
- return await this.gifToWebp(gif);
27
+ return await this.ToWebp(gif);
28
28
  } else if (this.mime === "image/gif") {
29
- return await this.gifToWebp(buffer);
29
+ return await this.ToWebp(buffer);
30
30
  } else if (this.mime.startsWith("image/")) {
31
- return await this.imageToWebp(buffer);
31
+ return await this.ToWebp(buffer);
32
32
  }
33
33
 
34
34
  throw new Error(`Unsupported file type: ${this.mime}`);
@@ -55,51 +55,24 @@ export default class MediaConverter {
55
55
  }
56
56
  }
57
57
 
58
- async gifToWebp(buffer) {
59
- const tempInput = join(tmpdir(), `input_${Date.now()}.gif`);
60
- const tempOutput = join(tmpdir(), `output_${Date.now()}.webp`);
61
-
62
- try {
63
- await fs.writeFile(tempInput, buffer);
64
-
65
- // Use gif2webp to convert GIF to WebP
66
- const gif2webpCmd = `gif2webp "${tempInput}" -q ${this.quality} -o "${tempOutput}"`;
67
- await exec(gif2webpCmd);
58
+ async ToWebp(buffer) {
68
59
 
69
- const webpBuffer = await fs.readFile(tempOutput);
60
+ const mimeType = this.mime;
61
+ const isAnimated = mimeType?.includes("video") || mimeType?.includes("gif");
70
62
 
71
- await Promise.all([fs.unlink(tempInput).catch(() => {}), fs.unlink(tempOutput).catch(() => {})]);
72
-
73
- return webpBuffer;
74
- } catch (error) {
75
- await Promise.all([fs.unlink(tempInput).catch(() => {}), fs.unlink(tempOutput).catch(() => {})]);
76
- throw new Error(`Failed to convert gif to webp: ${error.message}`);
77
- }
78
- }
63
+ // Initialize sharp with animation support for GIFs or video frames
64
+ const res = sharp(buffer, { animated: isAnimated });
79
65
 
80
- async imageToWebp(buffer) {
81
- const tempInput = join(tmpdir(), `input_${Date.now()}.png`);
82
- const tempOutput = join(tmpdir(), `output_${Date.now()}.webp`);
66
+ // Resize image to fit within the specified width while preserving aspect ratio
67
+ res.resize(this.width, this.width, {
68
+ fit: sharp.fit.contain,
69
+ background: { r: 0, g: 0, b: 0, alpha: 0 }, // Transparent background
70
+ });
83
71
 
84
- try {
85
- // Write the input buffer to a temporary file
86
- await fs.writeFile(tempInput, buffer);
87
-
88
- // Use cwebp to convert the image to WebP
89
- const cwebpCmd = `cwebp "${tempInput}" -q ${this.quality} -o "${tempOutput}"`;
90
- await exec(cwebpCmd);
91
-
92
- // Read the resulting WebP file as a buffer
93
- const webpBuffer = await fs.readFile(tempOutput);
94
-
95
- // Clean up temporary files
96
- await Promise.all([fs.unlink(tempInput).catch(() => {}), fs.unlink(tempOutput).catch(() => {})]);
97
-
98
- return webpBuffer; // Return the WebP buffer as output
99
- } catch (error) {
100
- // Clean up temporary files in case of error
101
- await Promise.all([fs.unlink(tempInput).catch(() => {}), fs.unlink(tempOutput).catch(() => {})]);
102
- throw new Error(`Failed to convert image to webp: ${error.message}`);
103
- }
72
+ // Convert to WebP format with the specified quality
73
+ return await res
74
+ .toFormat("webp")
75
+ .webp({ quality: this.quality, lossless: isAnimated }) // Lossless for GIFs or animations
76
+ .toBuffer();
104
77
  }
105
78
  }
Binary file
package/test/index.js DELETED
@@ -1,11 +0,0 @@
1
- import Sticker from '../index.js';
2
- import * as fs from 'fs';
3
- const sticker = new Sticker('./test/lol.mp4',{
4
- pack: 'My Pack',
5
- author: 'Me',
6
- category: ['😹'],
7
- quality: 80,
8
- text: 'Hello World'
9
- });
10
- sticker.metadata().then(console.log);
11
- fs.writeFileSync('sticker.webp', await sticker.build());
package/test/lol.mp4 DELETED
Binary file