@shibam/sticker-maker 1.0.5
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/index.d.ts +48 -0
- package/dist/index.js +230 -0
- package/dist/lib/ToWebp.d.ts +13 -0
- package/dist/lib/ToWebp.js +171 -0
- package/dist/lib/changeMetaInfo.d.ts +33 -0
- package/dist/lib/changeMetaInfo.js +144 -0
- package/dist/lib/extractMetaData.d.ts +7 -0
- package/dist/lib/extractMetaData.js +62 -0
- package/dist/lib/toGif.d.ts +1 -0
- package/dist/lib/toGif.js +67 -0
- package/dist/lib/utils.d.ts +39 -0
- package/dist/lib/utils.js +172 -0
- package/dist/types/StickerTypes.d.ts +6 -0
- package/dist/types/StickerTypes.js +10 -0
- package/dist/types/categoryType.d.ts +8 -0
- package/dist/types/categoryType.js +2 -0
- package/dist/types/metaInfoType.d.ts +11 -0
- package/dist/types/metaInfoType.js +2 -0
- package/package.json +45 -0
- package/readme.md +117 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { Readable } from 'stream';
|
|
4
|
+
import { MetaDataType } from './types/metaInfoType.js';
|
|
5
|
+
import { StickerTypes } from './types/StickerTypes.js';
|
|
6
|
+
declare class Sticker {
|
|
7
|
+
private data;
|
|
8
|
+
metaInfo: Partial<MetaDataType>;
|
|
9
|
+
private buffer;
|
|
10
|
+
private mimeType;
|
|
11
|
+
private extType;
|
|
12
|
+
private utils;
|
|
13
|
+
private outBuffer;
|
|
14
|
+
private activeBuff;
|
|
15
|
+
private activeMeta;
|
|
16
|
+
constructor(data: Buffer | string | Readable, metaInfo?: Partial<MetaDataType>);
|
|
17
|
+
/**
|
|
18
|
+
* Initializes the Sticker instance.
|
|
19
|
+
* - Sets default values for metaInfo.
|
|
20
|
+
* - Reads and analyzes input data to determine MIME type and extension type.
|
|
21
|
+
* - Generates ID and quality metadata if not provided.
|
|
22
|
+
*/
|
|
23
|
+
private initialize;
|
|
24
|
+
/**
|
|
25
|
+
* Converts input data to a Buffer containing the converted content.
|
|
26
|
+
* @returns Promise<Buffer> A Promise resolving to the converted content as Buffer.
|
|
27
|
+
*/
|
|
28
|
+
toBuffer(): Promise<Buffer>;
|
|
29
|
+
/**
|
|
30
|
+
* Converts input data and writes it to a file at the specified outputPath.
|
|
31
|
+
* @param outputPath The path where the converted file will be saved.
|
|
32
|
+
* @returns Promise<void> A Promise resolving when the file is successfully written.
|
|
33
|
+
*/
|
|
34
|
+
toFile(outputPath: string): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Changes the metadata of the sticker.
|
|
37
|
+
* @param newMetaInfo Partial metadata to update.
|
|
38
|
+
* @returns Promise<Buffer> A Promise resolving to the Buffer with updated metadata.
|
|
39
|
+
*/
|
|
40
|
+
changeMetaInfo(newMetaInfo?: Partial<MetaDataType>): Promise<Buffer>;
|
|
41
|
+
/**
|
|
42
|
+
* Extracts metadata from the provided data.
|
|
43
|
+
* @param data Buffer containing the data to extract metadata from.
|
|
44
|
+
* @returns Promise<Partial<MetaDataType>> A Promise resolving to the extracted metadata.
|
|
45
|
+
*/
|
|
46
|
+
extractMetaData(data: Buffer): Promise<Partial<MetaDataType>>;
|
|
47
|
+
}
|
|
48
|
+
export { Sticker, StickerTypes };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
exports.StickerTypes = exports.Sticker = void 0;
|
|
54
|
+
var fs_1 = __importDefault(require("fs"));
|
|
55
|
+
var utils_js_1 = __importDefault(require("./lib/utils.js"));
|
|
56
|
+
var ToWebp_js_1 = __importDefault(require("./lib/ToWebp.js"));
|
|
57
|
+
var StickerTypes_js_1 = require("./types/StickerTypes.js");
|
|
58
|
+
Object.defineProperty(exports, "StickerTypes", { enumerable: true, get: function () { return StickerTypes_js_1.StickerTypes; } });
|
|
59
|
+
var changeMetaInfo_js_1 = __importDefault(require("./lib/changeMetaInfo.js"));
|
|
60
|
+
var extractMetaData_js_1 = __importDefault(require("./lib/extractMetaData.js"));
|
|
61
|
+
var Sticker = /** @class */ (function () {
|
|
62
|
+
function Sticker(data, metaInfo) {
|
|
63
|
+
if (metaInfo === void 0) { metaInfo = {}; }
|
|
64
|
+
this.data = data;
|
|
65
|
+
this.metaInfo = metaInfo;
|
|
66
|
+
this.utils = new utils_js_1.default();
|
|
67
|
+
this.buffer = Buffer.from([]);
|
|
68
|
+
this.outBuffer = Buffer.from([]);
|
|
69
|
+
this.activeBuff = false;
|
|
70
|
+
this.activeMeta = false;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Initializes the Sticker instance.
|
|
74
|
+
* - Sets default values for metaInfo.
|
|
75
|
+
* - Reads and analyzes input data to determine MIME type and extension type.
|
|
76
|
+
* - Generates ID and quality metadata if not provided.
|
|
77
|
+
*/
|
|
78
|
+
Sticker.prototype.initialize = function () {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
80
|
+
var _a, fileType, error_1;
|
|
81
|
+
var _b, _c, _d, _e, _f, _g;
|
|
82
|
+
return __generator(this, function (_h) {
|
|
83
|
+
switch (_h.label) {
|
|
84
|
+
case 0:
|
|
85
|
+
_h.trys.push([0, 3, , 4]);
|
|
86
|
+
_a = this;
|
|
87
|
+
return [4 /*yield*/, this.utils.buffer(this.data)];
|
|
88
|
+
case 1:
|
|
89
|
+
_a.buffer = _h.sent();
|
|
90
|
+
return [4 /*yield*/, this.utils.getMimeType(this.buffer)];
|
|
91
|
+
case 2:
|
|
92
|
+
fileType = _h.sent();
|
|
93
|
+
this.mimeType = fileType === null || fileType === void 0 ? void 0 : fileType.mime;
|
|
94
|
+
this.extType = fileType === null || fileType === void 0 ? void 0 : fileType.ext;
|
|
95
|
+
this.metaInfo.pack = (_b = this.metaInfo.pack) !== null && _b !== void 0 ? _b : '';
|
|
96
|
+
this.metaInfo.author = (_c = this.metaInfo.author) !== null && _c !== void 0 ? _c : '';
|
|
97
|
+
this.metaInfo.id = (_d = this.metaInfo.id) !== null && _d !== void 0 ? _d : this.utils.getId();
|
|
98
|
+
this.metaInfo.category = (_e = this.metaInfo.category) !== null && _e !== void 0 ? _e : [];
|
|
99
|
+
this.metaInfo.type = (_f = this.metaInfo.type) !== null && _f !== void 0 ? _f : StickerTypes_js_1.StickerTypes.DEFAULT;
|
|
100
|
+
this.metaInfo.quality = (_g = this.metaInfo.quality) !== null && _g !== void 0 ? _g : this.utils.getQuality(this.buffer);
|
|
101
|
+
this.metaInfo.background = this.metaInfo.background;
|
|
102
|
+
return [3 /*break*/, 4];
|
|
103
|
+
case 3:
|
|
104
|
+
error_1 = _h.sent();
|
|
105
|
+
throw new Error("Initialization error: ".concat(error_1));
|
|
106
|
+
case 4: return [2 /*return*/];
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Converts input data to a Buffer containing the converted content.
|
|
113
|
+
* @returns Promise<Buffer> A Promise resolving to the converted content as Buffer.
|
|
114
|
+
*/
|
|
115
|
+
Sticker.prototype.toBuffer = function () {
|
|
116
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
117
|
+
var buffer, _a, error_2;
|
|
118
|
+
return __generator(this, function (_b) {
|
|
119
|
+
switch (_b.label) {
|
|
120
|
+
case 0:
|
|
121
|
+
_b.trys.push([0, 4, , 5]);
|
|
122
|
+
return [4 /*yield*/, this.initialize()];
|
|
123
|
+
case 1:
|
|
124
|
+
_b.sent();
|
|
125
|
+
return [4 /*yield*/, (0, ToWebp_js_1.default)(this.buffer, this.metaInfo, this.extType, this.mimeType)];
|
|
126
|
+
case 2:
|
|
127
|
+
buffer = _b.sent();
|
|
128
|
+
_a = this;
|
|
129
|
+
return [4 /*yield*/, new changeMetaInfo_js_1.default(this.metaInfo).add(buffer)];
|
|
130
|
+
case 3:
|
|
131
|
+
_a.outBuffer = _b.sent();
|
|
132
|
+
this.activeBuff = true;
|
|
133
|
+
return [2 /*return*/, this.outBuffer];
|
|
134
|
+
case 4:
|
|
135
|
+
error_2 = _b.sent();
|
|
136
|
+
this.activeBuff = false;
|
|
137
|
+
throw new Error("Conversion to buffer failed: ".concat(error_2));
|
|
138
|
+
case 5: return [2 /*return*/];
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Converts input data and writes it to a file at the specified outputPath.
|
|
145
|
+
* @param outputPath The path where the converted file will be saved.
|
|
146
|
+
* @returns Promise<void> A Promise resolving when the file is successfully written.
|
|
147
|
+
*/
|
|
148
|
+
Sticker.prototype.toFile = function (outputPath) {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
150
|
+
var error_3;
|
|
151
|
+
return __generator(this, function (_a) {
|
|
152
|
+
switch (_a.label) {
|
|
153
|
+
case 0:
|
|
154
|
+
_a.trys.push([0, 4, , 5]);
|
|
155
|
+
if (!(!this.activeBuff && !this.activeMeta)) return [3 /*break*/, 2];
|
|
156
|
+
return [4 /*yield*/, this.changeMetaInfo()];
|
|
157
|
+
case 1:
|
|
158
|
+
_a.sent();
|
|
159
|
+
_a.label = 2;
|
|
160
|
+
case 2: return [4 /*yield*/, fs_1.default.promises.writeFile(outputPath, this.outBuffer)];
|
|
161
|
+
case 3:
|
|
162
|
+
_a.sent();
|
|
163
|
+
return [3 /*break*/, 5];
|
|
164
|
+
case 4:
|
|
165
|
+
error_3 = _a.sent();
|
|
166
|
+
throw new Error("Conversion to file failed: ".concat(error_3));
|
|
167
|
+
case 5: return [2 /*return*/];
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Changes the metadata of the sticker.
|
|
174
|
+
* @param newMetaInfo Partial metadata to update.
|
|
175
|
+
* @returns Promise<Buffer> A Promise resolving to the Buffer with updated metadata.
|
|
176
|
+
*/
|
|
177
|
+
Sticker.prototype.changeMetaInfo = function () {
|
|
178
|
+
return __awaiter(this, arguments, void 0, function (newMetaInfo) {
|
|
179
|
+
var _a, error_4;
|
|
180
|
+
if (newMetaInfo === void 0) { newMetaInfo = {}; }
|
|
181
|
+
return __generator(this, function (_b) {
|
|
182
|
+
switch (_b.label) {
|
|
183
|
+
case 0:
|
|
184
|
+
_b.trys.push([0, 3, , 4]);
|
|
185
|
+
return [4 /*yield*/, this.initialize()];
|
|
186
|
+
case 1:
|
|
187
|
+
_b.sent();
|
|
188
|
+
this.metaInfo = __assign(__assign({}, this.metaInfo), newMetaInfo);
|
|
189
|
+
_a = this;
|
|
190
|
+
return [4 /*yield*/, new changeMetaInfo_js_1.default(this.metaInfo).add(this.buffer)];
|
|
191
|
+
case 2:
|
|
192
|
+
_a.outBuffer = _b.sent();
|
|
193
|
+
this.activeMeta = true;
|
|
194
|
+
return [2 /*return*/, this.outBuffer];
|
|
195
|
+
case 3:
|
|
196
|
+
error_4 = _b.sent();
|
|
197
|
+
this.activeMeta = false;
|
|
198
|
+
throw new Error("Error changing meta info: ".concat(error_4));
|
|
199
|
+
case 4: return [2 /*return*/];
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Extracts metadata from the provided data.
|
|
206
|
+
* @param data Buffer containing the data to extract metadata from.
|
|
207
|
+
* @returns Promise<Partial<MetaDataType>> A Promise resolving to the extracted metadata.
|
|
208
|
+
*/
|
|
209
|
+
Sticker.prototype.extractMetaData = function (data) {
|
|
210
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
211
|
+
var error_5;
|
|
212
|
+
return __generator(this, function (_a) {
|
|
213
|
+
switch (_a.label) {
|
|
214
|
+
case 0:
|
|
215
|
+
_a.trys.push([0, 2, , 3]);
|
|
216
|
+
return [4 /*yield*/, this.initialize()];
|
|
217
|
+
case 1:
|
|
218
|
+
_a.sent();
|
|
219
|
+
return [2 /*return*/, (0, extractMetaData_js_1.default)(data)];
|
|
220
|
+
case 2:
|
|
221
|
+
error_5 = _a.sent();
|
|
222
|
+
throw new Error("Error extracting meta data: ".concat(error_5));
|
|
223
|
+
case 3: return [2 /*return*/];
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
return Sticker;
|
|
229
|
+
}());
|
|
230
|
+
exports.Sticker = Sticker;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { MetaDataType } from '../types/metaInfoType.js';
|
|
3
|
+
/**
|
|
4
|
+
* Converts input buffer to WebP format with optional metadata and background color.
|
|
5
|
+
* @param buffer The input buffer to convert.
|
|
6
|
+
* @param metaInfo Optional metadata for the converted WebP.
|
|
7
|
+
* @param mimeExt File extension of the input buffer.
|
|
8
|
+
* @param mimeType MIME type of the input buffer.
|
|
9
|
+
* @returns Promise<Buffer | undefined> A Promise resolving to the converted content as Buffer.
|
|
10
|
+
* @throws Error if conversion fails.
|
|
11
|
+
*/
|
|
12
|
+
declare const ToWebp: (buffer: Buffer, metaInfo: Partial<MetaDataType>, mimeExt: string | undefined, mimeType: string | undefined) => Promise<Buffer>;
|
|
13
|
+
export default ToWebp;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
var child_process_1 = require("child_process");
|
|
43
|
+
var path_1 = require("path");
|
|
44
|
+
var promises_1 = require("fs/promises");
|
|
45
|
+
var os_1 = require("os");
|
|
46
|
+
var util_1 = require("util");
|
|
47
|
+
var StickerTypes_js_1 = require("../types/StickerTypes.js");
|
|
48
|
+
var toGif_js_1 = __importDefault(require("./toGif.js"));
|
|
49
|
+
var execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
50
|
+
/**
|
|
51
|
+
* Converts input buffer to WebP format with optional metadata and background color.
|
|
52
|
+
* @param buffer The input buffer to convert.
|
|
53
|
+
* @param metaInfo Optional metadata for the converted WebP.
|
|
54
|
+
* @param mimeExt File extension of the input buffer.
|
|
55
|
+
* @param mimeType MIME type of the input buffer.
|
|
56
|
+
* @returns Promise<Buffer | undefined> A Promise resolving to the converted content as Buffer.
|
|
57
|
+
* @throws Error if conversion fails.
|
|
58
|
+
*/
|
|
59
|
+
var ToWebp = function (buffer, metaInfo, mimeExt, mimeType) { return __awaiter(void 0, void 0, void 0, function () {
|
|
60
|
+
var inputPath, outputPath, _a, args, outputBuffer, error_1;
|
|
61
|
+
var _b, _c;
|
|
62
|
+
return __generator(this, function (_d) {
|
|
63
|
+
switch (_d.label) {
|
|
64
|
+
case 0:
|
|
65
|
+
inputPath = (0, path_1.join)((0, os_1.tmpdir)(), "".concat(Date.now(), "input.").concat(mimeExt));
|
|
66
|
+
outputPath = (0, path_1.join)((0, os_1.tmpdir)(), "".concat(Date.now(), "output.webp"));
|
|
67
|
+
_d.label = 1;
|
|
68
|
+
case 1:
|
|
69
|
+
_d.trys.push([1, 8, 9, 11]);
|
|
70
|
+
// Write input buffer to temporary file
|
|
71
|
+
return [4 /*yield*/, (0, promises_1.writeFile)(inputPath, buffer)];
|
|
72
|
+
case 2:
|
|
73
|
+
// Write input buffer to temporary file
|
|
74
|
+
_d.sent();
|
|
75
|
+
if (!(mimeType === null || mimeType === void 0 ? void 0 : mimeType.includes('video'))) return [3 /*break*/, 4];
|
|
76
|
+
return [4 /*yield*/, (0, toGif_js_1.default)(inputPath)];
|
|
77
|
+
case 3:
|
|
78
|
+
_a = _d.sent();
|
|
79
|
+
return [3 /*break*/, 5];
|
|
80
|
+
case 4:
|
|
81
|
+
_a = inputPath;
|
|
82
|
+
_d.label = 5;
|
|
83
|
+
case 5:
|
|
84
|
+
inputPath = _a;
|
|
85
|
+
args = [
|
|
86
|
+
'-i',
|
|
87
|
+
inputPath,
|
|
88
|
+
metaInfo.type === StickerTypes_js_1.StickerTypes.CIRCLE && !metaInfo.background
|
|
89
|
+
? StickerTypes_js_1.StickerTypes.CIRCLE
|
|
90
|
+
: metaInfo.type === StickerTypes_js_1.StickerTypes.SQUARE && !metaInfo.background
|
|
91
|
+
? StickerTypes_js_1.StickerTypes.SQUARE
|
|
92
|
+
: metaInfo.type === StickerTypes_js_1.StickerTypes.FIT && !metaInfo.background
|
|
93
|
+
? StickerTypes_js_1.StickerTypes.FIT
|
|
94
|
+
: '',
|
|
95
|
+
metaInfo.background
|
|
96
|
+
? "-filter_complex \"color=".concat(metaInfo.background, " [c]; [c][0]scale2ref[cs][0s];[cs][0s]overlay=shortest=1, unsharp=3:3:0.5").concat(metaInfo.type === StickerTypes_js_1.StickerTypes.CIRCLE ||
|
|
97
|
+
metaInfo.type === StickerTypes_js_1.StickerTypes.SQUARE ||
|
|
98
|
+
metaInfo.type === StickerTypes_js_1.StickerTypes.FIT
|
|
99
|
+
? ", ".concat(metaInfo.type.replace(/-vf/g, '').replace(/"/g, '').replace('-filter_complex', '').trim())
|
|
100
|
+
: '', "\"")
|
|
101
|
+
: '-pix_fmt yuva420p',
|
|
102
|
+
(mimeExt === null || mimeExt === void 0 ? void 0 : mimeExt.includes('gif')) || (mimeType === null || mimeType === void 0 ? void 0 : mimeType.includes('video'))
|
|
103
|
+
? [
|
|
104
|
+
'-c:v libwebp_anim',
|
|
105
|
+
'-auto-alt-ref 0',
|
|
106
|
+
'-lag-in-frames 0',
|
|
107
|
+
'-preset default',
|
|
108
|
+
'-t 10',
|
|
109
|
+
'-r 15',
|
|
110
|
+
'-loop 0',
|
|
111
|
+
'-metadata:s:v:0 alpha_mode="1"',
|
|
112
|
+
mimeExt === 'gif' ? '-lossless 1' : '-lossless 0'
|
|
113
|
+
].join(' ')
|
|
114
|
+
: '-c:v libwebp',
|
|
115
|
+
'-q:v',
|
|
116
|
+
(_c = (_b = metaInfo.quality) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : '50',
|
|
117
|
+
'-y',
|
|
118
|
+
outputPath
|
|
119
|
+
];
|
|
120
|
+
// Execute ffmpeg command with constructed arguments
|
|
121
|
+
return [4 /*yield*/, execAsync("ffmpeg ".concat(args.join(' ')))
|
|
122
|
+
// Read converted WebP file back into a buffer
|
|
123
|
+
];
|
|
124
|
+
case 6:
|
|
125
|
+
// Execute ffmpeg command with constructed arguments
|
|
126
|
+
_d.sent();
|
|
127
|
+
return [4 /*yield*/, (0, promises_1.readFile)(outputPath)];
|
|
128
|
+
case 7:
|
|
129
|
+
outputBuffer = _d.sent();
|
|
130
|
+
return [2 /*return*/, outputBuffer];
|
|
131
|
+
case 8:
|
|
132
|
+
error_1 = _d.sent();
|
|
133
|
+
console.error("Conversion error: ".concat(error_1));
|
|
134
|
+
throw new Error("Conversion failed: ".concat(error_1));
|
|
135
|
+
case 9:
|
|
136
|
+
// Cleanup temporary files
|
|
137
|
+
return [4 /*yield*/, cleanup(inputPath, outputPath).catch(function (e) { return console.error(e); })];
|
|
138
|
+
case 10:
|
|
139
|
+
// Cleanup temporary files
|
|
140
|
+
_d.sent();
|
|
141
|
+
return [7 /*endfinally*/];
|
|
142
|
+
case 11: return [2 /*return*/];
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}); };
|
|
146
|
+
/**
|
|
147
|
+
* Cleans up temporary files used during conversion.
|
|
148
|
+
* @param inputPath Path to the input temporary file.
|
|
149
|
+
* @param outputPath Path to the output temporary file.
|
|
150
|
+
*/
|
|
151
|
+
function cleanup(inputPath, outputPath) {
|
|
152
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
153
|
+
var unlinkError_1;
|
|
154
|
+
return __generator(this, function (_a) {
|
|
155
|
+
switch (_a.label) {
|
|
156
|
+
case 0:
|
|
157
|
+
_a.trys.push([0, 2, , 3]);
|
|
158
|
+
return [4 /*yield*/, Promise.all([(0, promises_1.unlink)(inputPath), (0, promises_1.unlink)(outputPath)])];
|
|
159
|
+
case 1:
|
|
160
|
+
_a.sent();
|
|
161
|
+
return [3 /*break*/, 3];
|
|
162
|
+
case 2:
|
|
163
|
+
unlinkError_1 = _a.sent();
|
|
164
|
+
console.error('Error cleaning output file:', unlinkError_1);
|
|
165
|
+
return [3 /*break*/, 3];
|
|
166
|
+
case 3: return [2 /*return*/];
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
exports.default = ToWebp;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference path="../../src/lib/node-webpmux.d.ts" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import Image from 'node-webpmux';
|
|
4
|
+
/**
|
|
5
|
+
* The Exif class is responsible for handling the metadata (Exif data)
|
|
6
|
+
* for sticker images, particularly those used in messaging applications.
|
|
7
|
+
*/
|
|
8
|
+
export default class Exif {
|
|
9
|
+
private data;
|
|
10
|
+
private exif;
|
|
11
|
+
/**
|
|
12
|
+
* Constructs an Exif instance with the given options.
|
|
13
|
+
* @param options - An object containing metadata for the sticker.
|
|
14
|
+
*/
|
|
15
|
+
constructor(options: any);
|
|
16
|
+
/**
|
|
17
|
+
* Builds the Exif metadata as a Buffer.
|
|
18
|
+
* @returns A Buffer containing the constructed Exif data.
|
|
19
|
+
*/
|
|
20
|
+
build: () => Buffer;
|
|
21
|
+
/**
|
|
22
|
+
* Adds the Exif metadata to the given image.
|
|
23
|
+
* @param image - A Buffer or Image instance representing the image to which Exif data should be added.
|
|
24
|
+
* @returns A Promise that resolves to a Buffer containing the image with the added Exif data.
|
|
25
|
+
*/
|
|
26
|
+
add: (image: Buffer | Image.Image) => Promise<Buffer>;
|
|
27
|
+
/**
|
|
28
|
+
* Loads the image from a Buffer or string and returns an Image instance.
|
|
29
|
+
* @param image - A Buffer or string representing the image to be loaded.
|
|
30
|
+
* @returns A Promise that resolves to an Image instance.
|
|
31
|
+
*/
|
|
32
|
+
load: (image: Buffer | string) => Promise<Image.Image>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
var node_webpmux_1 = __importDefault(require("node-webpmux"));
|
|
54
|
+
var util_1 = require("util");
|
|
55
|
+
/**
|
|
56
|
+
* The Exif class is responsible for handling the metadata (Exif data)
|
|
57
|
+
* for sticker images, particularly those used in messaging applications.
|
|
58
|
+
*/
|
|
59
|
+
var Exif = /** @class */ (function () {
|
|
60
|
+
/**
|
|
61
|
+
* Constructs an Exif instance with the given options.
|
|
62
|
+
* @param options - An object containing metadata for the sticker.
|
|
63
|
+
*/
|
|
64
|
+
function Exif(options) {
|
|
65
|
+
var _this = this;
|
|
66
|
+
this.data = {};
|
|
67
|
+
this.exif = null;
|
|
68
|
+
/**
|
|
69
|
+
* Builds the Exif metadata as a Buffer.
|
|
70
|
+
* @returns A Buffer containing the constructed Exif data.
|
|
71
|
+
*/
|
|
72
|
+
this.build = function () {
|
|
73
|
+
var data = JSON.stringify(_this.data);
|
|
74
|
+
// Construct the Exif header and data sections.
|
|
75
|
+
var exif = Buffer.concat([
|
|
76
|
+
Buffer.from([
|
|
77
|
+
0x49, 0x49, 0x2a, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00,
|
|
78
|
+
0x00, 0x16, 0x00, 0x00, 0x00
|
|
79
|
+
]),
|
|
80
|
+
Buffer.from(data, 'utf-8')
|
|
81
|
+
]);
|
|
82
|
+
// Write the length of the data into the Exif header.
|
|
83
|
+
exif.writeUIntLE(new util_1.TextEncoder().encode(data).length, 14, 4);
|
|
84
|
+
return exif;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Adds the Exif metadata to the given image.
|
|
88
|
+
* @param image - A Buffer or Image instance representing the image to which Exif data should be added.
|
|
89
|
+
* @returns A Promise that resolves to a Buffer containing the image with the added Exif data.
|
|
90
|
+
*/
|
|
91
|
+
this.add = function (image) { return __awaiter(_this, void 0, void 0, function () {
|
|
92
|
+
var exif, _a;
|
|
93
|
+
return __generator(this, function (_b) {
|
|
94
|
+
switch (_b.label) {
|
|
95
|
+
case 0:
|
|
96
|
+
exif = this.exif || this.build();
|
|
97
|
+
if (!(image instanceof node_webpmux_1.default.Image)) return [3 /*break*/, 1];
|
|
98
|
+
_a = image;
|
|
99
|
+
return [3 /*break*/, 3];
|
|
100
|
+
case 1: return [4 /*yield*/, this.load(image)
|
|
101
|
+
// Set the Exif data on the image and save it.
|
|
102
|
+
];
|
|
103
|
+
case 2:
|
|
104
|
+
_a = _b.sent();
|
|
105
|
+
_b.label = 3;
|
|
106
|
+
case 3:
|
|
107
|
+
// Load the image if it is not already an instance of Image.Image.
|
|
108
|
+
image = _a;
|
|
109
|
+
// Set the Exif data on the image and save it.
|
|
110
|
+
image.exif = exif;
|
|
111
|
+
return [4 /*yield*/, image.save(null)];
|
|
112
|
+
case 4: return [2 /*return*/, _b.sent()];
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}); };
|
|
116
|
+
/**
|
|
117
|
+
* Loads the image from a Buffer or string and returns an Image instance.
|
|
118
|
+
* @param image - A Buffer or string representing the image to be loaded.
|
|
119
|
+
* @returns A Promise that resolves to an Image instance.
|
|
120
|
+
*/
|
|
121
|
+
this.load = function (image) { return __awaiter(_this, void 0, void 0, function () {
|
|
122
|
+
var img;
|
|
123
|
+
return __generator(this, function (_a) {
|
|
124
|
+
switch (_a.label) {
|
|
125
|
+
case 0:
|
|
126
|
+
img = new node_webpmux_1.default.Image();
|
|
127
|
+
return [4 /*yield*/, img.load(image)];
|
|
128
|
+
case 1:
|
|
129
|
+
_a.sent();
|
|
130
|
+
return [2 /*return*/, img];
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
}); };
|
|
134
|
+
this.data['sticker-pack-id'] = options.id;
|
|
135
|
+
this.data['sticker-pack-name'] = options.pack || '';
|
|
136
|
+
this.data['sticker-pack-publisher'] = options.author || '';
|
|
137
|
+
this.data['emojis'] = options.category || [];
|
|
138
|
+
this.data['sticker-quality'] = options.quality || '30';
|
|
139
|
+
this.data['sticker-background'] = options.background || '';
|
|
140
|
+
this.data = __assign(__assign({}, this.data), options);
|
|
141
|
+
}
|
|
142
|
+
return Exif;
|
|
143
|
+
}());
|
|
144
|
+
exports.default = Exif;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
var node_webpmux_1 = __importDefault(require("node-webpmux"));
|
|
43
|
+
/**
|
|
44
|
+
* Extracts metadata from a WebP image.
|
|
45
|
+
* @param {Buffer}image - The image buffer to extract metadata from
|
|
46
|
+
*/
|
|
47
|
+
var extractMetadata = function (image) { return __awaiter(void 0, void 0, void 0, function () {
|
|
48
|
+
var img, exif;
|
|
49
|
+
var _a, _b, _c;
|
|
50
|
+
return __generator(this, function (_d) {
|
|
51
|
+
switch (_d.label) {
|
|
52
|
+
case 0:
|
|
53
|
+
img = new node_webpmux_1.default.Image();
|
|
54
|
+
return [4 /*yield*/, img.load(image)];
|
|
55
|
+
case 1:
|
|
56
|
+
_d.sent();
|
|
57
|
+
exif = (_b = (_a = img.exif) === null || _a === void 0 ? void 0 : _a.toString('utf-8')) !== null && _b !== void 0 ? _b : '{}';
|
|
58
|
+
return [2 /*return*/, JSON.parse((_c = exif.substring(exif.indexOf('{'), exif.lastIndexOf('}') + 1)) !== null && _c !== void 0 ? _c : '{}')];
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}); };
|
|
62
|
+
exports.default = extractMetadata;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function toGif(inputPath: string): Promise<string>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
var child_process_1 = require("child_process");
|
|
40
|
+
var util_1 = require("util");
|
|
41
|
+
var os_1 = require("os");
|
|
42
|
+
var path_1 = require("path");
|
|
43
|
+
var execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
44
|
+
function toGif(inputPath) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
46
|
+
var outputPath, args, error_1;
|
|
47
|
+
return __generator(this, function (_a) {
|
|
48
|
+
switch (_a.label) {
|
|
49
|
+
case 0:
|
|
50
|
+
outputPath = (0, path_1.join)((0, os_1.tmpdir)(), "".concat(Date.now(), ".gif"));
|
|
51
|
+
_a.label = 1;
|
|
52
|
+
case 1:
|
|
53
|
+
_a.trys.push([1, 3, , 4]);
|
|
54
|
+
args = "ffmpeg -i ".concat(inputPath, " -vf \"fps=10,scale=320:-1:flags=lanczos\" -t 7 -c:v gif -r 15 -preset slow -pix_fmt rgb24 ").concat(outputPath);
|
|
55
|
+
return [4 /*yield*/, execAsync(args)];
|
|
56
|
+
case 2:
|
|
57
|
+
_a.sent();
|
|
58
|
+
return [2 /*return*/, outputPath !== null && outputPath !== void 0 ? outputPath : ''];
|
|
59
|
+
case 3:
|
|
60
|
+
error_1 = _a.sent();
|
|
61
|
+
throw Error("Error converting to gif: ".concat(error_1));
|
|
62
|
+
case 4: return [2 /*return*/];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
exports.default = toGif;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { Readable } from 'stream';
|
|
4
|
+
export default class Utils {
|
|
5
|
+
constructor();
|
|
6
|
+
/**
|
|
7
|
+
* Converts various types of input data to a Buffer.
|
|
8
|
+
* @param data Input data as Buffer, string (file path), or Readable stream.
|
|
9
|
+
* @returns Promise<Buffer> A Promise resolving to the converted Buffer.
|
|
10
|
+
* @throws Error if conversion fails.
|
|
11
|
+
*/
|
|
12
|
+
buffer(data: Buffer | string | Readable): Promise<Buffer>;
|
|
13
|
+
/**
|
|
14
|
+
* Converts a Readable stream to a Buffer.
|
|
15
|
+
* @param stream Readable stream to convert.
|
|
16
|
+
* @returns Promise<Buffer> A Promise resolving to the converted Buffer.
|
|
17
|
+
*/
|
|
18
|
+
streamToBuffer(stream: Readable): Promise<Buffer>;
|
|
19
|
+
/**
|
|
20
|
+
* Determines the quality of the data based on its size.
|
|
21
|
+
* @param data Buffer containing the data.
|
|
22
|
+
* @returns number Quality value based on data size.
|
|
23
|
+
*/
|
|
24
|
+
getQuality(data: Buffer): number;
|
|
25
|
+
/**
|
|
26
|
+
* Determines the MIME type of the data buffer.
|
|
27
|
+
* @param data Buffer containing the data.
|
|
28
|
+
* @returns Promise<{ mime: string; ext: string }> A Promise resolving to an object with MIME type and extension.
|
|
29
|
+
*/
|
|
30
|
+
getMimeType(data: Buffer): Promise<{
|
|
31
|
+
mime: string;
|
|
32
|
+
ext: string;
|
|
33
|
+
} | undefined>;
|
|
34
|
+
/**
|
|
35
|
+
* Generates a random ID.
|
|
36
|
+
* @returns string A random alphanumeric ID.
|
|
37
|
+
*/
|
|
38
|
+
getId(): string;
|
|
39
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
39
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
40
|
+
if (ar || !(i in from)) {
|
|
41
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
42
|
+
ar[i] = from[i];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
46
|
+
};
|
|
47
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
48
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
49
|
+
};
|
|
50
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
+
var stream_1 = require("stream");
|
|
52
|
+
var fs_1 = __importDefault(require("fs"));
|
|
53
|
+
var file_type_1 = require("file-type");
|
|
54
|
+
var Utils = /** @class */ (function () {
|
|
55
|
+
function Utils() {
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Converts various types of input data to a Buffer.
|
|
59
|
+
* @param data Input data as Buffer, string (file path), or Readable stream.
|
|
60
|
+
* @returns Promise<Buffer> A Promise resolving to the converted Buffer.
|
|
61
|
+
* @throws Error if conversion fails.
|
|
62
|
+
*/
|
|
63
|
+
Utils.prototype.buffer = function (data) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
65
|
+
var buffer, _a, _b, error_1;
|
|
66
|
+
return __generator(this, function (_c) {
|
|
67
|
+
switch (_c.label) {
|
|
68
|
+
case 0:
|
|
69
|
+
_c.trys.push([0, 7, , 8]);
|
|
70
|
+
if (!(typeof data === 'string')) return [3 /*break*/, 2];
|
|
71
|
+
return [4 /*yield*/, fs_1.default.promises.readFile(data)];
|
|
72
|
+
case 1:
|
|
73
|
+
_a = _c.sent();
|
|
74
|
+
return [3 /*break*/, 6];
|
|
75
|
+
case 2:
|
|
76
|
+
if (!(data instanceof stream_1.Readable)) return [3 /*break*/, 4];
|
|
77
|
+
return [4 /*yield*/, this.streamToBuffer(data)];
|
|
78
|
+
case 3:
|
|
79
|
+
_b = _c.sent();
|
|
80
|
+
return [3 /*break*/, 5];
|
|
81
|
+
case 4:
|
|
82
|
+
_b = Buffer.from(data);
|
|
83
|
+
_c.label = 5;
|
|
84
|
+
case 5:
|
|
85
|
+
_a = _b;
|
|
86
|
+
_c.label = 6;
|
|
87
|
+
case 6:
|
|
88
|
+
buffer = _a;
|
|
89
|
+
return [2 /*return*/, buffer];
|
|
90
|
+
case 7:
|
|
91
|
+
error_1 = _c.sent();
|
|
92
|
+
throw new Error("Error converting to buffer: ".concat(error_1));
|
|
93
|
+
case 8: return [2 /*return*/];
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* Converts a Readable stream to a Buffer.
|
|
100
|
+
* @param stream Readable stream to convert.
|
|
101
|
+
* @returns Promise<Buffer> A Promise resolving to the converted Buffer.
|
|
102
|
+
*/
|
|
103
|
+
Utils.prototype.streamToBuffer = function (stream) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
105
|
+
var chunks;
|
|
106
|
+
return __generator(this, function (_a) {
|
|
107
|
+
chunks = [];
|
|
108
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
109
|
+
stream.on('data', function (chunk) {
|
|
110
|
+
if (Buffer.isBuffer(chunk)) {
|
|
111
|
+
chunks.push(chunk);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
chunks.push(Buffer.from(chunk));
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
stream.on('end', function () {
|
|
118
|
+
resolve(Buffer.concat(chunks));
|
|
119
|
+
});
|
|
120
|
+
stream.on('error', function (err) {
|
|
121
|
+
reject(err);
|
|
122
|
+
});
|
|
123
|
+
})];
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Determines the quality of the data based on its size.
|
|
129
|
+
* @param data Buffer containing the data.
|
|
130
|
+
* @returns number Quality value based on data size.
|
|
131
|
+
*/
|
|
132
|
+
Utils.prototype.getQuality = function (data) {
|
|
133
|
+
var buffer = Buffer.from(data);
|
|
134
|
+
var bytes = buffer.length / 1024;
|
|
135
|
+
var quality = bytes > 4 * 1024 ? 8 : bytes > 3 * 1024 ? 10 : bytes > 2 * 1024 ? 12 : 15;
|
|
136
|
+
return quality;
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Determines the MIME type of the data buffer.
|
|
140
|
+
* @param data Buffer containing the data.
|
|
141
|
+
* @returns Promise<{ mime: string; ext: string }> A Promise resolving to an object with MIME type and extension.
|
|
142
|
+
*/
|
|
143
|
+
Utils.prototype.getMimeType = function (data) {
|
|
144
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
145
|
+
var fileType, error_2;
|
|
146
|
+
return __generator(this, function (_a) {
|
|
147
|
+
switch (_a.label) {
|
|
148
|
+
case 0:
|
|
149
|
+
_a.trys.push([0, 2, , 3]);
|
|
150
|
+
return [4 /*yield*/, (0, file_type_1.fromBuffer)(data)];
|
|
151
|
+
case 1:
|
|
152
|
+
fileType = _a.sent();
|
|
153
|
+
return [2 /*return*/, fileType];
|
|
154
|
+
case 2:
|
|
155
|
+
error_2 = _a.sent();
|
|
156
|
+
console.error("Error getting MIME type: ".concat(error_2));
|
|
157
|
+
return [2 /*return*/, undefined];
|
|
158
|
+
case 3: return [2 /*return*/];
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Generates a random ID.
|
|
165
|
+
* @returns string A random alphanumeric ID.
|
|
166
|
+
*/
|
|
167
|
+
Utils.prototype.getId = function () {
|
|
168
|
+
return __spreadArray([], Array(5), true).map(function () { return Math.random().toString(36).substring(2, 15); }).join('');
|
|
169
|
+
};
|
|
170
|
+
return Utils;
|
|
171
|
+
}());
|
|
172
|
+
exports.default = Utils;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare enum StickerTypes {
|
|
2
|
+
DEFAULT = "",
|
|
3
|
+
SQUARE = "-vf \"crop=min(iw\\,ih):min(iw\\,ih)\"",
|
|
4
|
+
CIRCLE = "-filter_complex \"crop=min(iw\\,ih):min(iw\\,ih), scale=iw*2:ih*2, format=rgba, geq=r='r(X\\,Y)':g='g(X\\,Y)':b='b(X\\,Y)':a='if(lte(sqrt((X-W/2)^2 + (Y-H/2)^2)\\, min(W\\,H)/2)\\, 255\\, 0)'\"",
|
|
5
|
+
FIT = "-vf \"scale=iw*min(200/iw\\,200/ih):ih*min(200/iw\\,200/ih),pad=200:200:(200-iw*min(200/iw\\,200/ih))/2:(200-ih*min(200/iw\\,200/ih))/2\""
|
|
6
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StickerTypes = void 0;
|
|
4
|
+
var StickerTypes;
|
|
5
|
+
(function (StickerTypes) {
|
|
6
|
+
StickerTypes["DEFAULT"] = "";
|
|
7
|
+
StickerTypes["SQUARE"] = "-vf \"crop=min(iw\\,ih):min(iw\\,ih)\"";
|
|
8
|
+
StickerTypes["CIRCLE"] = "-filter_complex \"crop=min(iw\\,ih):min(iw\\,ih), scale=iw*2:ih*2, format=rgba, geq=r='r(X\\,Y)':g='g(X\\,Y)':b='b(X\\,Y)':a='if(lte(sqrt((X-W/2)^2 + (Y-H/2)^2)\\, min(W\\,H)/2)\\, 255\\, 0)'\"";
|
|
9
|
+
StickerTypes["FIT"] = "-vf \"scale=iw*min(200/iw\\,200/ih):ih*min(200/iw\\,200/ih),pad=200:200:(200-iw*min(200/iw\\,200/ih))/2:(200-ih*min(200/iw\\,200/ih))/2\"";
|
|
10
|
+
})(StickerTypes || (exports.StickerTypes = StickerTypes = {}));
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type LoveEmojis = '❤' | '😍' | '😘' | '💕' | '😻' | '💑' | '👩❤👩' | '👨❤👨' | '💏' | '👩❤💋👩' | '👨❤💋👨' | '🧡' | '💛' | '💚' | '💙' | '💜' | '🖤' | '💔' | '❣' | '💞' | '💓' | '💗' | '💖' | '💘' | '💝' | '💟' | '♥' | '💌' | '💋' | '👩❤️💋👩' | '👨❤️💋👨' | '👩❤️👨' | '👩❤️👩' | '👨❤️👨' | '👩❤️💋👨' | '👬' | '👭' | '👫' | '🥰' | '😚' | '😙' | '👄' | '🌹' | '😽' | '❣️' | '❤️';
|
|
2
|
+
type HappyEmojis = '😀' | '😃' | '😄' | '😁' | '😆' | '😅' | '😂' | '🤣' | '🙂' | '😛' | '😝' | '😜' | '🤪' | '🤗' | '😺' | '😸' | '😹' | '☺' | '😌' | '😉' | '🤗' | '😊';
|
|
3
|
+
type SadEmojis = '☹' | '😣' | '😖' | '😫' | '😩' | '😢' | '😭' | '😞' | '😔' | '😟' | '😕' | '😤' | '😠' | '😥' | '😰' | '😨' | '😿' | '😾' | '😓' | '🙍♂️' | '🙍♀️' | '💔' | '🙁' | '🥺' | '🤕' | '☔️' | '⛈' | '🌩' | '🌧';
|
|
4
|
+
type AngryEmojis = '😯' | '😦' | '😧' | '😮' | '😲' | '🙀' | '😱' | '🤯' | '😳' | '❗' | '❕' | '🤬' | '😡' | '😠' | '🙄' | '👿' | '😾' | '😤' | '💢' | '👺' | '🗯️' | '😒' | '🥵';
|
|
5
|
+
type GreetEmojis = '👋';
|
|
6
|
+
type CelebrateEmojis = '🎊' | '🎉' | '🎁' | '🎈' | '👯♂️' | '👯' | '👯♀️' | '💃' | '🕺' | '🔥' | '⭐️' | '✨' | '💫' | '🎇' | '🎆' | '🍻' | '🥂' | '🍾' | '🎂' | '🍰';
|
|
7
|
+
export type category = LoveEmojis | HappyEmojis | SadEmojis | AngryEmojis | GreetEmojis | CelebrateEmojis;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { category } from './categoryType.js';
|
|
2
|
+
import { StickerTypes } from './StickerTypes.js';
|
|
3
|
+
export interface MetaDataType {
|
|
4
|
+
pack?: string;
|
|
5
|
+
author?: string;
|
|
6
|
+
id?: string | null;
|
|
7
|
+
category?: category[];
|
|
8
|
+
type?: StickerTypes | string;
|
|
9
|
+
quality?: number;
|
|
10
|
+
background?: String | undefined;
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shibam/sticker-maker",
|
|
3
|
+
"version": "1.0.5",
|
|
4
|
+
"description": "A package for creating stickers",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "commonjs",
|
|
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/NekoSenpai69/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
|
+
"file-type": "16.5.0",
|
|
37
|
+
"node-webpmux": "^3.2.0",
|
|
38
|
+
"prettier": "^3.3.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/node": "^20.10.0",
|
|
42
|
+
"tsx": "^4.7.1",
|
|
43
|
+
"typescript": "^5.4.5"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
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. 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
|
+
# ⚠️ Before installing this make sure you have downloaded ffmpeg
|
|
11
|
+
|
|
12
|
+
You can install the package using npm:
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install @shibam/sticker-maker
|
|
16
|
+
```
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Here's how you can use the `Sticker` class:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import fs from "fs";
|
|
23
|
+
import { Readable } from "stream";
|
|
24
|
+
import { Sticker, StickerTypes } from "@shibam/sticker-maker";
|
|
25
|
+
|
|
26
|
+
// Example 1: Create a new sticker instance and convert to buffer
|
|
27
|
+
const sticker = new Sticker("path/to/image.png", {
|
|
28
|
+
pack: "My Sticker Pack",
|
|
29
|
+
author: "Shibam",
|
|
30
|
+
id: "123467890",
|
|
31
|
+
category: ['😂','😹'],
|
|
32
|
+
type: StickerTypes.DEFAULT,
|
|
33
|
+
quality: 30,
|
|
34
|
+
background: "red"
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
const buffer = await sticker.toBuffer();
|
|
39
|
+
console.log("Sticker converted to buffer:", buffer);
|
|
40
|
+
} catch (error) {
|
|
41
|
+
console.error("Error converting sticker to buffer:", error);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Example 2: Create a new sticker instance and convert to file
|
|
45
|
+
const sticker2 = new Sticker("path/to/another/image.png", {
|
|
46
|
+
pack: "Another Sticker Pack",
|
|
47
|
+
author: "John Doe",
|
|
48
|
+
id: "987654321",
|
|
49
|
+
category: ['😊','👍'],
|
|
50
|
+
type: StickerTypes.CIRCLE,
|
|
51
|
+
quality: 50,
|
|
52
|
+
background: "blue"
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
await sticker2.toFile("path/to/output.webp");
|
|
57
|
+
console.log("Sticker converted and saved to file successfully.");
|
|
58
|
+
} catch (error) {
|
|
59
|
+
console.error("Error converting sticker to file:", error);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Class: `Sticker`
|
|
64
|
+
|
|
65
|
+
### Constructor
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
new Sticker(data: Buffer | string | Readable, metaInfo?: Partial<MetaDataType>)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
- `data`: Input data for the sticker. Can be a Buffer, file path as a string, or Readable stream.
|
|
72
|
+
- `metaInfo` (optional): Metadata about the sticker.
|
|
73
|
+
|
|
74
|
+
### Methods
|
|
75
|
+
|
|
76
|
+
#### `toBuffer(): Promise<Buffer>`
|
|
77
|
+
|
|
78
|
+
Converts input data to a Buffer containing the converted sticker content.
|
|
79
|
+
|
|
80
|
+
#### `toFile(outputPath: string): Promise<void>`
|
|
81
|
+
|
|
82
|
+
Converts input data and writes the converted sticker to a file at `outputPath`.
|
|
83
|
+
|
|
84
|
+
#### `changeMetaInfo(newMetaInfo: Partial<any>): Promise<any | undefined>`
|
|
85
|
+
|
|
86
|
+
Updates metadata information with `newMetaInfo` and applies changes to the sticker.
|
|
87
|
+
|
|
88
|
+
#### `extractMetaData(data: Buffer): Promise<any>`
|
|
89
|
+
|
|
90
|
+
Extracts metadata from `data` and returns the extracted information.
|
|
91
|
+
|
|
92
|
+
## Types
|
|
93
|
+
|
|
94
|
+
### `StickerTypes`
|
|
95
|
+
|
|
96
|
+
An enum specifying different types of stickers:
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
enum StickerTypes {
|
|
100
|
+
DEFAULT,
|
|
101
|
+
CIRCLE,
|
|
102
|
+
SQUARE,
|
|
103
|
+
FIT
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## License
|
|
108
|
+
|
|
109
|
+
This package is licensed under the MIT License.
|
|
110
|
+
|
|
111
|
+
## Contributing
|
|
112
|
+
|
|
113
|
+
Contributions are welcome. Feel free to open issues or submit pull requests on [GitHub](https://github.com/your/repository).
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
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.
|