@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.
- package/dist/lib/ToWebp.js +4 -2
- package/dist/lib/textOnGif.js +6 -6
- package/dist/lib/textOnImg.js +14 -6
- package/dist/lib/toGif.js +12 -3
- package/package.json +48 -50
- package/readme.md +111 -112
package/dist/lib/ToWebp.js
CHANGED
|
@@ -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
|
-
:
|
|
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) {
|
package/dist/lib/textOnGif.js
CHANGED
|
@@ -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:
|
|
8
|
-
font_color:
|
|
9
|
-
font_family:
|
|
10
|
-
stroke_color:
|
|
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
|
}
|
package/dist/lib/textOnImg.js
CHANGED
|
@@ -3,7 +3,7 @@ export default class TextOnImage {
|
|
|
3
3
|
fontSize;
|
|
4
4
|
maxCharsPerLine;
|
|
5
5
|
constructor(maxCharsPerLine = 25) {
|
|
6
|
-
this.fontSize =
|
|
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
|
-
|
|
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
|
-
|
|
42
|
-
|
|
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
|
-
|
|
47
|
-
|
|
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([
|
|
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.
|
|
4
|
-
"description": "A package for creating stickers",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"type": "module",
|
|
7
|
-
"files": [
|
|
8
|
-
"dist"
|
|
9
|
-
],
|
|
10
|
-
"scripts": {
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
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
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
console.
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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.
|