@midscene/shared 0.7.2 → 0.7.3-beta-20241104100519.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/browser/constants.js +41 -0
- package/dist/{types → browser}/fs.d.ts +2 -2
- package/dist/browser/fs.js +62 -0
- package/dist/{types → browser}/img.d.ts +23 -9
- package/dist/browser/img.js +395 -0
- package/dist/browser/index.d.ts +3 -0
- package/dist/browser/index.js +26 -0
- package/dist/browser/utils.d.ts +4 -0
- package/dist/browser/utils.js +34 -0
- package/dist/es/constants.d.ts +13 -0
- package/dist/es/fs.d.ts +14 -0
- package/dist/es/fs.js +17 -14
- package/dist/es/img.d.ts +143 -0
- package/dist/es/img.js +271 -187
- package/dist/es/index.d.ts +3 -0
- package/dist/es/index.js +1 -3
- package/dist/es/utils.d.ts +4 -0
- package/dist/es/utils.js +13 -0
- package/dist/lib/constants.d.ts +13 -0
- package/dist/lib/fs.d.ts +14 -0
- package/dist/lib/fs.js +18 -25
- package/dist/lib/img.d.ts +143 -0
- package/dist/lib/img.js +270 -185
- package/dist/lib/index.d.ts +3 -0
- package/dist/lib/index.js +1 -3
- package/dist/lib/utils.d.ts +4 -0
- package/dist/lib/utils.js +39 -0
- package/dist/types/index.d.ts +2 -2
- package/package.json +37 -20
- package/src/fs/index.ts +16 -12
- package/src/img/box-select.ts +185 -170
- package/src/img/get-jimp.ts +11 -0
- package/src/img/index.ts +2 -0
- package/src/img/info.ts +24 -6
- package/src/img/jimp.d.ts +4 -0
- package/src/img/transform.ts +31 -9
- package/src/index.ts +1 -3
- package/src/utils.ts +10 -0
- /package/dist/{types → browser}/constants.d.ts +0 -0
package/src/img/transform.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
|
-
import
|
|
2
|
+
import getJimp from './get-jimp';
|
|
3
3
|
|
|
4
|
+
/**
|
|
4
5
|
/**
|
|
5
6
|
* Saves a Base64-encoded image to a file
|
|
6
7
|
*
|
|
@@ -21,6 +22,7 @@ export async function saveBase64Image(options: {
|
|
|
21
22
|
const imageBuffer = Buffer.from(base64Image, 'base64');
|
|
22
23
|
|
|
23
24
|
// Use Jimp to process the image and save it to the specified location
|
|
25
|
+
const Jimp = await getJimp();
|
|
24
26
|
const image = await Jimp.read(imageBuffer);
|
|
25
27
|
await image.writeAsync(outputPath);
|
|
26
28
|
}
|
|
@@ -32,6 +34,7 @@ export async function saveBase64Image(options: {
|
|
|
32
34
|
*/
|
|
33
35
|
export async function transformImgPathToBase64(inputPath: string) {
|
|
34
36
|
// Use Jimp to process images and generate base64 data
|
|
37
|
+
const Jimp = await getJimp();
|
|
35
38
|
const image = await Jimp.read(inputPath);
|
|
36
39
|
const buffer = await image.getBufferAsync(Jimp.MIME_PNG);
|
|
37
40
|
return buffer.toString('base64');
|
|
@@ -45,18 +48,17 @@ export async function transformImgPathToBase64(inputPath: string) {
|
|
|
45
48
|
* @throws An error if the width or height cannot be determined from the metadata
|
|
46
49
|
*/
|
|
47
50
|
export async function resizeImg(
|
|
48
|
-
inputData:
|
|
51
|
+
inputData: Buffer,
|
|
49
52
|
newSize?: {
|
|
50
53
|
width: number;
|
|
51
54
|
height: number;
|
|
52
55
|
},
|
|
53
|
-
): Promise<
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
? Buffer.from(inputData.split(';base64,').pop() || inputData, 'base64')
|
|
57
|
-
: inputData;
|
|
56
|
+
): Promise<Buffer> {
|
|
57
|
+
if (typeof inputData === 'string')
|
|
58
|
+
throw Error('inputData is base64, use resizeImgBase64 instead');
|
|
58
59
|
|
|
59
|
-
const
|
|
60
|
+
const Jimp = await getJimp();
|
|
61
|
+
const image = await Jimp.read(inputData);
|
|
60
62
|
const { width, height } = image.bitmap;
|
|
61
63
|
|
|
62
64
|
if (!width || !height) {
|
|
@@ -68,7 +70,26 @@ export async function resizeImg(
|
|
|
68
70
|
image.resize(finalNewSize.width, finalNewSize.height);
|
|
69
71
|
const resizedBuffer = await image.getBufferAsync(Jimp.MIME_PNG);
|
|
70
72
|
|
|
71
|
-
return
|
|
73
|
+
return resizedBuffer;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function resizeImgBase64(
|
|
77
|
+
inputData: string,
|
|
78
|
+
newSize?: {
|
|
79
|
+
width: number;
|
|
80
|
+
height: number;
|
|
81
|
+
},
|
|
82
|
+
): Promise<string> {
|
|
83
|
+
const splitFlag = ';base64,';
|
|
84
|
+
const dataSplitted = inputData.split(splitFlag);
|
|
85
|
+
if (dataSplitted.length !== 2) {
|
|
86
|
+
throw Error('Invalid base64 data');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const imageBuffer = Buffer.from(dataSplitted[1], 'base64');
|
|
90
|
+
const buffer = await resizeImg(imageBuffer, newSize);
|
|
91
|
+
const content = buffer.toString('base64');
|
|
92
|
+
return `${dataSplitted[0]}${splitFlag}${content}`;
|
|
72
93
|
}
|
|
73
94
|
|
|
74
95
|
/**
|
|
@@ -127,6 +148,7 @@ export async function trimImage(image: string | Buffer): Promise<{
|
|
|
127
148
|
width: number;
|
|
128
149
|
height: number;
|
|
129
150
|
} | null> {
|
|
151
|
+
const Jimp = await getJimp();
|
|
130
152
|
const jimpImage = await Jimp.read(
|
|
131
153
|
Buffer.isBuffer(image) ? image : Buffer.from(image),
|
|
132
154
|
);
|
package/src/index.ts
CHANGED
package/src/utils.ts
ADDED
|
File without changes
|