@shibam/sticker-maker 1.3.0-rc7 → 1.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shibam/sticker-maker",
3
- "version": "1.3.0-rc7",
3
+ "version": "1.3.0",
4
4
  "description": "A package for creating whatsapp stickers",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/readme.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @shibam/sticker-maker
2
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 (⁠≧⁠▽⁠≦⁠).
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 dependency. 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
4
 
5
5
  # Sticker Class
6
6
 
@@ -20,10 +20,10 @@ Here's how you can use the `Sticker` class:
20
20
  ```typescript
21
21
  import fs from "fs";
22
22
  import { Readable } from "stream";
23
- import { Sticker, StickerTypes } from "@shibam/sticker-maker";
23
+ import Sticker, { StickerTypes } from "@shibam/sticker-maker";
24
24
 
25
25
  // Example 1: Create a new sticker instance and convert to buffer
26
- const sticker = new Sticker("path/to/image.png", {
26
+ const sticker = new Sticker("path/to/image.png", {// input can be file path or buffer or stream
27
27
  pack: "My Sticker Pack",
28
28
  author: "Shibam",
29
29
  id: "123467890",
@@ -41,7 +41,7 @@ try {
41
41
  }
42
42
 
43
43
  // Example 2: Create a new sticker instance and convert to file
44
- const sticker2 = new Sticker("path/to/another/image.png", {
44
+ const sticker2 = new Sticker("path/to/another/image.png", {// input can be file path or buffer or stream
45
45
  pack: "Another Sticker Pack",
46
46
  author: "John Doe",
47
47
  id: "987654321",
@@ -81,7 +81,7 @@ Converts input data to a Buffer containing the converted sticker content.
81
81
  Converts input data and writes the converted sticker to a file at `outputPath`.
82
82
 
83
83
 
84
- #### `extractMetaData(data: Buffer): Promise<any>`
84
+ # `extractMetaData(data: Buffer): Promise<any>`
85
85
 
86
86
  Extracts metadata from `data` and returns the extracted information.
87
87
 
package/src/lib/ToWebp.js CHANGED
@@ -4,6 +4,7 @@ import fs from 'fs-extra'
4
4
  import os from 'os'
5
5
  import path from 'path'
6
6
  import toGif from './toGif.js'
7
+ import toPng from './toPng.js'
7
8
  import TextOnImg from './textOnImg.js'
8
9
 
9
10
  const exec = promisify(execFile)
@@ -11,7 +12,7 @@ const textOnImg = new TextOnImg()
11
12
 
12
13
  export default async function ToWebp(buffer, metaInfo, mimeExt, mimeType) {
13
14
  const timestamp = Date.now()
14
- const inputPath = path.join(os.tmpdir(), `input_${timestamp}.${mimeExt}`)
15
+ const inputPath = path.join(os.tmpdir(), `input_${timestamp}.png`)
15
16
  const outputPath = path.join(os.tmpdir(), `output_${timestamp}.webp`)
16
17
 
17
18
  try {
@@ -27,6 +28,9 @@ export default async function ToWebp(buffer, metaInfo, mimeExt, mimeType) {
27
28
  } else if (metaInfo.text) {
28
29
  processedData = await textOnImg.drawText(buffer, metaInfo.text)
29
30
  currentExt = 'png'
31
+ } else {
32
+ processedData = await toPng(buffer, mimeExt)
33
+ currentExt = 'png'
30
34
  }
31
35
 
32
36
  await fs.writeFile(inputPath, processedData)
@@ -52,9 +56,9 @@ export default async function ToWebp(buffer, metaInfo, mimeExt, mimeType) {
52
56
  '-c:v',
53
57
  'libwebp',
54
58
  '-quality',
55
- String(metaInfo.quality || 80),
59
+ String(metaInfo.quality || 90),
56
60
  '-lossless',
57
- currentExt === 'gif' ? '1' : '0',
61
+ '0',
58
62
  outputPath
59
63
  )
60
64
 
@@ -77,7 +77,7 @@ export default class TextOnGif {
77
77
  const [width, height] = stdout.trim().split('x').map(Number)
78
78
  if (!width || !height) throw new Error('Invalid GIF dimensions')
79
79
 
80
- const fontSize = Math.floor(height / 10)
80
+ const fontSize = Math.floor(height / 8)
81
81
  const lineHeight = fontSize * 1.2
82
82
  const lines = this.wrapText(text, this.maxCharsPerLine)
83
83
 
@@ -77,7 +77,7 @@ export default class TextOnImg {
77
77
  const [width, height] = stdout.trim().split('x').map(Number)
78
78
  if (!width || !height) throw new Error('Invalid image dimensions')
79
79
 
80
- const fontSize = Math.floor(height / 10)
80
+ const fontSize = Math.floor(height / 7)
81
81
  const lineHeight = fontSize * 1.2
82
82
  const lines = this.wrapText(text, this.maxCharsPerLine)
83
83
 
package/src/lib/toGif.js CHANGED
@@ -17,9 +17,9 @@ export default async function videoToGif(buffer, extType, type, text = '') {
17
17
  try {
18
18
  await fs.writeFile(inputPath, buffer)
19
19
  const vf = type === 'SQUARE'
20
- ? 'scale=320:-1:flags=lanczos,fps=10,crop=min(iw\\,ih):min(iw\\,ih)'
21
- : 'scale=320:-1:flags=lanczos,fps=20'
22
- await exec('ffmpeg', ['-y', '-i', inputPath, '-vf', vf, '-t', '7', '-loop', '0', '-f', 'gif', outputPath])
20
+ ? "scale=320:-1:flags=lanczos,fps=10,crop=min(iw\\,ih):min(iw\\,ih)"
21
+ : "scale=320:-1:flags=lanczos,fps=10"
22
+ await exec('ffmpeg', ['-y', '-i', inputPath, '-vf', vf, '-t', '6', '-loop', '0', '-f', 'gif', outputPath])
23
23
  let result = await fs.readFile(outputPath)
24
24
  if (text) result = await textOnGif.drawText(result, text)
25
25
  return result
@@ -0,0 +1,23 @@
1
+ import { promisify } from 'util'
2
+ import { execFile } from 'child_process'
3
+ import fs from 'fs-extra'
4
+ import os from 'os'
5
+ import path from 'path'
6
+ const exec = promisify(execFile)
7
+
8
+ export default async function toPng(buffer, mimeExt) {
9
+ if (mimeExt === 'png') return buffer
10
+ const timestamp = Date.now()
11
+ const inputPath = path.join(os.tmpdir(), `input_${timestamp}.${mimeExt}`)
12
+ const outputPath = path.join(os.tmpdir(), `output_${timestamp}.png`)
13
+ try {
14
+ await fs.writeFile(inputPath, buffer)
15
+ await exec('ffmpeg', ['-y', '-i', inputPath, outputPath])
16
+ const result = await fs.readFile(outputPath)
17
+ return result
18
+ } catch (err) {
19
+ throw new Error(`Failed to convert to PNG: ${err.message}`)
20
+ } finally {
21
+ await Promise.all([fs.remove(inputPath), fs.remove(outputPath)]).catch(() => {})
22
+ }
23
+ }
package/src/lib/utils.js CHANGED
@@ -20,7 +20,7 @@ export default class Utils {
20
20
  }
21
21
  getQuality(data) {
22
22
  const bytes = Buffer.from(data).length / 1024
23
- return bytes > 4096 ? 8 : bytes > 3072 ? 10 : bytes > 2048 ? 12 : 15
23
+ return bytes > 4096 ? 70 : bytes > 3072 ? 80 : bytes > 2048 ? 85 : 90
24
24
  }
25
25
  async getMimeType(data) {
26
26
  try {