@shibam/sticker-maker 1.1.14 → 1.2.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.
@@ -16,9 +16,11 @@ const ToWebp = async (buffer, metaInfo, mimeExt, mimeType) => {
16
16
  try {
17
17
  if (mimeExt === 'webp')
18
18
  return buffer;
19
- let data = mimeType?.includes('video')
19
+ let data = (mimeType?.includes('video')
20
20
  ? await toGif(buffer, mimeExt, metaInfo.type || StickerTypes.DEFAULT, metaInfo.text ?? '')
21
- : (metaInfo.text ? await textOnImg.drawText(buffer, metaInfo.text) : buffer);
21
+ : metaInfo.text
22
+ ? await textOnImg.drawText(buffer, metaInfo.text)
23
+ : buffer);
22
24
  let isAnimated = mimeType?.includes('video') || mimeExt?.includes('gif');
23
25
  const res = sharp(data, { animated: isAnimated });
24
26
  if (metaInfo.type === StickerTypes.CIRCLE) {
@@ -4,15 +4,15 @@ const textOnGif = async (fileName, text) => {
4
4
  try {
5
5
  const gif = new TextOnGif({
6
6
  file_path: fileName,
7
- font_size: "32px",
8
- font_color: "white",
9
- font_family: "Arial",
10
- stroke_color: "black",
11
- stroke_width: 3,
7
+ font_size: '32px',
8
+ font_color: 'white',
9
+ font_family: 'Arial',
10
+ stroke_color: 'black',
11
+ stroke_width: 3
12
12
  });
13
13
  const buff = await gif.textOnGif({
14
14
  text,
15
- get_as_buffer: true,
15
+ get_as_buffer: true
16
16
  });
17
17
  resolve(buff);
18
18
  }
@@ -3,7 +3,7 @@ export default class TextOnImage {
3
3
  fontSize;
4
4
  maxCharsPerLine;
5
5
  constructor(maxCharsPerLine = 25) {
6
- this.fontSize = 125;
6
+ this.fontSize = 175;
7
7
  this.maxCharsPerLine = maxCharsPerLine;
8
8
  }
9
9
  wrapText(ctx, text) {
@@ -13,7 +13,8 @@ export default class TextOnImage {
13
13
  for (let i = 1; i < words.length; i++) {
14
14
  const word = words[i];
15
15
  const testLine = `${currentLine} ${word}`;
16
- if (testLine.length > this.maxCharsPerLine) {
16
+ const { width: testWidth } = ctx.measureText(testLine);
17
+ if (testWidth > ctx.canvas.width * 0.9) {
17
18
  lines.push(currentLine);
18
19
  currentLine = word;
19
20
  }
@@ -28,23 +29,30 @@ export default class TextOnImage {
28
29
  const image = await loadImage(imageBuffer);
29
30
  const canvas = createCanvas(image.width, image.height);
30
31
  const ctx = canvas.getContext('2d');
32
+ // Draw the image on canvas
31
33
  ctx.drawImage(image, 0, 0);
34
+ // Calculate font size based on image height
35
+ this.fontSize = Math.floor(image.height / 10);
32
36
  ctx.font = `bold ${this.fontSize}px Arial`;
33
37
  ctx.fillStyle = 'white';
34
38
  ctx.textAlign = 'center';
35
39
  ctx.textBaseline = 'top';
36
40
  ctx.lineWidth = 4;
37
41
  ctx.strokeStyle = 'black';
42
+ // Wrap the text to fit within the canvas width
38
43
  const lines = this.wrapText(ctx, text);
39
44
  const lineHeight = this.fontSize * 1.2;
40
45
  const totalTextHeight = lines.length * lineHeight;
41
- if (totalTextHeight + (2 * padding.y) > canvas.height) {
42
- this.fontSize = (canvas.height - (2 * padding.y)) / (lines.length * 1.2);
46
+ // If text height exceeds image height, adjust font size
47
+ if (totalTextHeight + 2 * padding.y > canvas.height) {
48
+ this.fontSize = Math.floor((canvas.height - 2 * padding.y) / (lines.length * 1.2));
43
49
  ctx.font = `bold ${this.fontSize}px Arial`;
44
50
  }
51
+ // Start text positioning at the bottom, above padding
45
52
  let y = canvas.height - totalTextHeight - padding.y;
46
- lines.forEach(line => {
47
- let x = canvas.width / 2;
53
+ // Draw each line of text with stroke and fill for better contrast
54
+ lines.forEach((line) => {
55
+ const x = canvas.width / 2;
48
56
  ctx.strokeText(line, x, y);
49
57
  ctx.fillText(line, x, y);
50
58
  y += lineHeight;
package/dist/lib/toGif.js CHANGED
@@ -1,11 +1,9 @@
1
1
  import ffmpeg from 'fluent-ffmpeg';
2
- import ffmpegInstaller from '@ffmpeg-installer/ffmpeg';
3
2
  import { tmpdir } from 'os';
4
3
  import { writeFile, readFile, unlink } from 'fs/promises';
5
4
  import { join } from 'path';
6
5
  import { StickerTypes } from '../types/StickerTypes.js';
7
6
  import TextOnGif from './textOnGif.js';
8
- ffmpeg.setFfmpegPath(ffmpegInstaller.path);
9
7
  const videoToGif = (buffer, extType, type, text = '') => {
10
8
  return new Promise(async (resolve, reject) => {
11
9
  const execute = async (attempt) => {
@@ -20,7 +18,18 @@ const videoToGif = (buffer, extType, type, text = '') => {
20
18
  await new Promise((resolveFfmpeg, rejectFfmpeg) => {
21
19
  ffmpeg(filename)
22
20
  .inputFormat(extType)
23
- .outputOptions(['-vf', shape, '-loop', '0', '-lossless', '0', '-t', '7', '-preset', 'ultrafast'])
21
+ .outputOptions([
22
+ '-vf',
23
+ shape,
24
+ '-loop',
25
+ '0',
26
+ '-lossless',
27
+ '0',
28
+ '-t',
29
+ '7',
30
+ '-preset',
31
+ 'ultrafast'
32
+ ])
24
33
  .toFormat('gif')
25
34
  .save(outputFilename)
26
35
  .on('end', resolveFfmpeg)
package/package.json CHANGED
@@ -1,50 +1,48 @@
1
- {
2
- "name": "@shibam/sticker-maker",
3
- "version": "1.1.14",
4
- "description": "A package for creating stickers",
5
- "main": "dist/index.js",
6
- "type": "module",
7
- "files": [
8
- "dist"
9
- ],
10
- "scripts": {
11
- "prebuild": "rm -rf dist",
12
- "build": "tsc -p .",
13
- "lint": "eslint \"src/**/*.ts\"",
14
- "prepublish": "npm run build",
15
- "fmt": "prettier --config .prettierrc \"**/*.{ts,mjs}\" --write",
16
- "test": "mocha --timeout 60000 -r ts-node/register \"tests/**/*.test.ts\"",
17
- "clean:webp": "find ./ -name '*.webp' -delete",
18
- "release": "release-it"
19
- },
20
- "author": "Shibam Dey",
21
- "license": "MIT",
22
- "repository": {
23
- "type": "git",
24
- "url": "https://github.com/ShibamDey69/Sticker-Maker.git"
25
- },
26
- "keywords": [
27
- "stickers",
28
- "sticker maker",
29
- "image processing",
30
- "wa sticker maker",
31
- "wasticker",
32
- "nodejs",
33
- "typescript"
34
- ],
35
- "dependencies": {
36
- "@ffmpeg-installer/ffmpeg": "^1.1.0",
37
- "@types/fluent-ffmpeg": "^2.1.24",
38
- "canvas": "^2.11.2",
39
- "file-type": "^19.4.0",
40
- "fluent-ffmpeg": "^2.1.3",
41
- "node-webpmux": "^3.2.0",
42
- "sharp": "^0.33.4",
43
- "text-on-gif": "^2.0.13"
44
- },
45
- "devDependencies": {
46
- "@types/node": "^20.10.0",
47
- "tsx": "^4.7.1",
48
- "typescript": "^5.4.5"
49
- }
50
- }
1
+ {
2
+ "name": "@shibam/sticker-maker",
3
+ "version": "1.2.0",
4
+ "description": "A package for creating stickers",
5
+ "main": "dist/index.js",
6
+ "type": "module",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc -p .",
12
+ "lint": "eslint \"src/**/*.ts\"",
13
+ "prepublish": "npm run build",
14
+ "fmt": "prettier --config .prettierrc \"**/*.{ts,mjs}\" --write",
15
+ "test": "mocha --timeout 60000 -r ts-node/register \"tests/**/*.test.ts\"",
16
+ "clean:webp": "find ./ -name '*.webp' -delete",
17
+ "release": "release-it"
18
+ },
19
+ "author": "Shibam Dey",
20
+ "license": "MIT",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/ShibamDey69/Sticker-Maker.git"
24
+ },
25
+ "keywords": [
26
+ "stickers",
27
+ "sticker maker",
28
+ "image processing",
29
+ "wa sticker maker",
30
+ "wasticker",
31
+ "nodejs",
32
+ "typescript"
33
+ ],
34
+ "dependencies": {
35
+ "@types/fluent-ffmpeg": "^2.1.24",
36
+ "canvas": "^2.11.2",
37
+ "file-type": "^19.4.0",
38
+ "fluent-ffmpeg": "^2.1.3",
39
+ "node-webpmux": "^3.2.0",
40
+ "sharp": "^0.33.4",
41
+ "text-on-gif": "^2.0.13"
42
+ },
43
+ "devDependencies": {
44
+ "@types/node": "^20.17.14",
45
+ "tsx": "^4.7.1",
46
+ "typescript": "^5.4.5"
47
+ }
48
+ }
package/readme.md CHANGED
@@ -1,113 +1,112 @@
1
- # @shibam/sticker-maker
2
-
3
- `@shibam/sticker-maker` is a lightweight utility library designed for converting images and videos into stickers while allowing customization of metadata. you don't have to download ffmpeg in terminal. It supports various input types and ensures high-quality sticker conversion. This module has minimal dependencies, ensuring efficient performance. If you encounter any issues, please feel free to open an issue. However, please check if a similar issue has already been reported before creating a new one. Happy Coding (⁠≧⁠▽⁠≦⁠).
4
-
5
- # Sticker Class
6
-
7
- The `Sticker` class is a utility for converting images and videos into sticker format, with options for metadata customization and manipulation.
8
-
9
- ## Installation
10
-
11
- You can install the package using npm:
12
-
13
- ```sh
14
- npm install @shibam/sticker-maker
15
- ```
16
- ## Usage
17
-
18
- Here's how you can use the `Sticker` class:
19
-
20
- ```typescript
21
- import fs from "fs";
22
- import { Readable } from "stream";
23
- import { Sticker, StickerTypes } from "@shibam/sticker-maker";
24
-
25
- // Example 1: Create a new sticker instance and convert to buffer
26
- const sticker = new Sticker("path/to/image.png", {
27
- pack: "My Sticker Pack",
28
- author: "Shibam",
29
- id: "123467890",
30
- category: ['😂','😹'],
31
- type: StickerTypes.DEFAULT,
32
- quality: 30,
33
- });
34
-
35
- try {
36
- const buffer = await sticker.toBuffer();
37
- console.log("Sticker converted to buffer:", buffer);
38
- } catch (error) {
39
- console.error("Error converting sticker to buffer:", error);
40
- }
41
-
42
- // Example 2: Create a new sticker instance and convert to file
43
- const sticker2 = new Sticker("path/to/another/image.png", {
44
- pack: "Another Sticker Pack",
45
- author: "John Doe",
46
- id: "987654321",
47
- category: ['😊','👍'],
48
- type: StickerTypes.CIRCLE,
49
- quality: 50,
50
- });
51
-
52
- try {
53
- await sticker2.toFile("path/to/output.webp");
54
- console.log("Sticker converted and saved to file successfully.");
55
- } catch (error) {
56
- console.error("Error converting sticker to file:", error);
57
- }
58
- ```
59
-
60
- ## Class: `Sticker`
61
-
62
- ### Constructor
63
-
64
- ```typescript
65
- new Sticker(data: Buffer | string | Readable, metaInfo?: Partial<MetaDataType>)
66
- ```
67
-
68
- - `data`: Input data for the sticker. Can be a Buffer, file path as a string, or Readable stream.
69
- - `metaInfo` (optional): Metadata about the sticker.
70
-
71
- ### Methods
72
-
73
- #### `toBuffer(): Promise<Buffer>`
74
-
75
- Converts input data to a Buffer containing the converted sticker content.
76
-
77
- #### `toFile(outputPath: string): Promise<void>`
78
-
79
- Converts input data and writes the converted sticker to a file at `outputPath`.
80
-
81
- #### `changeMetaInfo(newMetaInfo: Partial<any>): Promise<any | undefined>`
82
-
83
- Updates metadata information with `newMetaInfo` and applies changes to the sticker.
84
-
85
- #### `extractMetaData(data: Buffer): Promise<any>`
86
-
87
- Extracts metadata from `data` and returns the extracted information.
88
-
89
- ## Types
90
-
91
- ### `StickerTypes`
92
-
93
- An enum specifying different types of stickers:
94
-
95
- ```typescript
96
- enum StickerTypes {
97
- DEFAULT,
98
- CIRCLE,
99
- SQUARE,
100
- }
101
- ```
102
-
103
- ## License
104
-
105
- This package is licensed under the MIT License.
106
-
107
- ## Contributing
108
-
109
- Contributions are welcome. Feel free to open issues or submit pull requests on [GitHub](https://github.com/NekoSenpai69/Sticker-Maker).
110
-
111
- ---
112
-
1
+ # @shibam/sticker-maker
2
+
3
+ `@shibam/sticker-maker` is a lightweight utility library designed for converting images and videos into stickers while allowing customization of metadata. you have to download ffmpeg and canvas dependencies globally. It supports various input types and ensures high-quality sticker conversion. This module has minimal dependencies, ensuring efficient performance. If you encounter any issues, please feel free to open an issue. However, please check if a similar issue has already been reported before creating a new one. Happy Coding (⁠≧⁠▽⁠≦⁠).
4
+
5
+ # Sticker Class
6
+
7
+ The `Sticker` class is a utility for converting images and videos into sticker format, with options for metadata customization and manipulation.
8
+
9
+ ## Installation
10
+
11
+ You can install the package using npm:
12
+
13
+ ```sh
14
+ npm install @shibam/sticker-maker
15
+ ```
16
+ ## Usage
17
+
18
+ Here's how you can use the `Sticker` class:
19
+
20
+ ```typescript
21
+ import fs from "fs";
22
+ import { Readable } from "stream";
23
+ import { Sticker, StickerTypes } from "@shibam/sticker-maker";
24
+
25
+ // Example 1: Create a new sticker instance and convert to buffer
26
+ const sticker = new Sticker("path/to/image.png", {
27
+ pack: "My Sticker Pack",
28
+ author: "Shibam",
29
+ id: "123467890",
30
+ category: ['😂','😹'],
31
+ type: StickerTypes.DEFAULT,
32
+ quality: 30,
33
+ text:"hello baka!" // if you want to use this download canvas dependecies
34
+ });
35
+
36
+ try {
37
+ const buffer = await sticker.toBuffer();
38
+ console.log("Sticker converted to buffer:", buffer);
39
+ } catch (error) {
40
+ console.error("Error converting sticker to buffer:", error);
41
+ }
42
+
43
+ // Example 2: Create a new sticker instance and convert to file
44
+ const sticker2 = new Sticker("path/to/another/image.png", {
45
+ pack: "Another Sticker Pack",
46
+ author: "John Doe",
47
+ id: "987654321",
48
+ category: ['😊','👍'],
49
+ type: StickerTypes.CIRCLE,
50
+ quality: 50,
51
+ text:"hello baka!" // if you want to use this download canvas dependecies
52
+ });
53
+
54
+ try {
55
+ await sticker2.toFile("path/to/output.webp");
56
+ console.log("Sticker converted and saved to file successfully.");
57
+ } catch (error) {
58
+ console.error("Error converting sticker to file:", error);
59
+ }
60
+ ```
61
+
62
+ ## Class: `Sticker`
63
+
64
+ ### Constructor
65
+
66
+ ```typescript
67
+ new Sticker(data: Buffer | string | Readable, metaInfo?: Partial<MetaDataType>)
68
+ ```
69
+
70
+ - `data`: Input data for the sticker. Can be a Buffer, file path as a string, or Readable stream.
71
+ - `metaInfo` (optional): Metadata about the sticker.
72
+
73
+ ### Methods
74
+
75
+ #### `toBuffer(): Promise<Buffer>`
76
+
77
+ Converts input data to a Buffer containing the converted sticker content.
78
+
79
+ #### `toFile(outputPath: string): Promise<void>`
80
+
81
+ Converts input data and writes the converted sticker to a file at `outputPath`.
82
+
83
+
84
+ #### `extractMetaData(data: Buffer): Promise<any>`
85
+
86
+ Extracts metadata from `data` and returns the extracted information.
87
+
88
+ ## Types
89
+
90
+ ### `StickerTypes`
91
+
92
+ An enum specifying different types of stickers:
93
+
94
+ ```typescript
95
+ enum StickerTypes {
96
+ DEFAULT,
97
+ CIRCLE,
98
+ SQUARE,
99
+ }
100
+ ```
101
+
102
+ ## License
103
+
104
+ This package is licensed under the MIT License.
105
+
106
+ ## Contributing
107
+
108
+ Contributions are welcome. Feel free to open issues or submit pull requests on [GitHub](https://github.com/NekoSenpai69/Sticker-Maker).
109
+
110
+ ---
111
+
113
112
  This README provides an overview of the `Sticker` class functionalities, installation instructions, examples of usage, information about types used, and guidelines for contributing to the project. Adjust paths and additional details based on your actual implementation and repository setup.