@jsarmyknife/native--file 1.0.2-beta.0 → 2.0.0-beta.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/web.d.ts +8 -0
- package/dist/web.js +33 -22
- package/package.json +1 -1
- package/web.ts +40 -29
package/dist/web.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
export declare function convertFileToBase64(file: File): Promise<string>;
|
|
2
2
|
export declare function convertBase64ToFile(base64: string, filename?: string): File;
|
|
3
3
|
export declare function getFileLink(fileInput: HTMLInputElement, fileIndex?: number): string | null;
|
|
4
|
+
export declare function resizeAndCropImage({ rawImage, downsizingTo, targetCrop }: {
|
|
5
|
+
rawImage: File;
|
|
6
|
+
downsizingTo?: {
|
|
7
|
+
height: number;
|
|
8
|
+
width: number;
|
|
9
|
+
};
|
|
10
|
+
targetCrop?: `${number}x${number}`;
|
|
11
|
+
}): Promise<File>;
|
package/dist/web.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.convertFileToBase64 = convertFileToBase64;
|
|
4
4
|
exports.convertBase64ToFile = convertBase64ToFile;
|
|
5
5
|
exports.getFileLink = getFileLink;
|
|
6
|
+
exports.resizeAndCropImage = resizeAndCropImage;
|
|
6
7
|
function convertFileToBase64(file) {
|
|
7
8
|
return new Promise((resolve, reject) => {
|
|
8
9
|
const reader = new FileReader();
|
|
@@ -43,7 +44,7 @@ function getFileLink(fileInput, fileIndex = 0) {
|
|
|
43
44
|
let blobURL = URL.createObjectURL(fileInput.files[fileIndex]);
|
|
44
45
|
return blobURL;
|
|
45
46
|
}
|
|
46
|
-
async function resizeAndCropImage({ rawImage,
|
|
47
|
+
async function resizeAndCropImage({ rawImage, downsizingTo, targetCrop }) {
|
|
47
48
|
// Create image element from file
|
|
48
49
|
const img = new Image();
|
|
49
50
|
const imageUrl = URL.createObjectURL(rawImage);
|
|
@@ -52,35 +53,45 @@ async function resizeAndCropImage({ rawImage, downsize, targetAspect }) {
|
|
|
52
53
|
img.onerror = reject;
|
|
53
54
|
img.src = imageUrl;
|
|
54
55
|
});
|
|
55
|
-
//
|
|
56
|
-
const [aspectWidth, aspectHeight] = targetAspect.split('x').map(Number);
|
|
57
|
-
const targetRatio = aspectWidth / aspectHeight;
|
|
58
|
-
// Target dimensions
|
|
59
|
-
const targetWidth = downsize.width;
|
|
60
|
-
const targetHeight = downsize.height;
|
|
61
|
-
// Calculate source image ratio
|
|
62
|
-
const imgRatio = img.width / img.height;
|
|
56
|
+
// Step 1: Calculate the dimensions after optional cropping
|
|
63
57
|
let sourceX = 0;
|
|
64
58
|
let sourceY = 0;
|
|
65
59
|
let sourceWidth = img.width;
|
|
66
60
|
let sourceHeight = img.height;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
61
|
+
if (targetCrop) {
|
|
62
|
+
// Parse target aspect ratio (e.g., "2x3" -> 2/3)
|
|
63
|
+
const [aspectWidth, aspectHeight] = targetCrop.split('x').map(Number);
|
|
64
|
+
const targetRatio = aspectWidth / aspectHeight;
|
|
65
|
+
const imgRatio = img.width / img.height;
|
|
66
|
+
// Center crop to target ratio
|
|
67
|
+
if (imgRatio > targetRatio) {
|
|
68
|
+
// Image is wider - crop width
|
|
69
|
+
sourceWidth = img.height * targetRatio;
|
|
70
|
+
sourceX = (img.width - sourceWidth) / 2;
|
|
71
|
+
}
|
|
72
|
+
else if (imgRatio < targetRatio) {
|
|
73
|
+
// Image is taller - crop height
|
|
74
|
+
sourceHeight = img.width / targetRatio;
|
|
75
|
+
sourceY = (img.height - sourceHeight) / 2;
|
|
76
|
+
}
|
|
72
77
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
78
|
+
// Step 2: Calculate canvas dimensions after optional downsizing
|
|
79
|
+
let canvasWidth = sourceWidth;
|
|
80
|
+
let canvasHeight = sourceHeight;
|
|
81
|
+
if (downsizingTo) {
|
|
82
|
+
// Scale down to fit within bounds while maintaining aspect ratio
|
|
83
|
+
const scaleX = downsizingTo.width / sourceWidth;
|
|
84
|
+
const scaleY = downsizingTo.height / sourceHeight;
|
|
85
|
+
const scale = Math.min(scaleX, scaleY, 1); // Only downsize, never upsize
|
|
86
|
+
canvasWidth = sourceWidth * scale;
|
|
87
|
+
canvasHeight = sourceHeight * scale;
|
|
77
88
|
}
|
|
78
|
-
// Draw
|
|
89
|
+
// Draw image on canvas (no stretching, fills entire canvas)
|
|
79
90
|
const canvas = document.createElement('canvas');
|
|
80
|
-
canvas.width =
|
|
81
|
-
canvas.height =
|
|
91
|
+
canvas.width = canvasWidth;
|
|
92
|
+
canvas.height = canvasHeight;
|
|
82
93
|
const ctx = canvas.getContext('2d');
|
|
83
|
-
ctx.drawImage(img, sourceX, sourceY, sourceWidth, sourceHeight, 0, 0,
|
|
94
|
+
ctx.drawImage(img, sourceX, sourceY, sourceWidth, sourceHeight, 0, 0, canvasWidth, canvasHeight);
|
|
84
95
|
// Clean up
|
|
85
96
|
URL.revokeObjectURL(imageUrl);
|
|
86
97
|
// Convert canvas to blob then to File
|
package/package.json
CHANGED
package/web.ts
CHANGED
|
@@ -43,14 +43,14 @@ export function getFileLink(fileInput: HTMLInputElement, fileIndex = 0) {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
|
|
46
|
-
async function resizeAndCropImage({
|
|
46
|
+
export async function resizeAndCropImage({
|
|
47
47
|
rawImage,
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
downsizingTo,
|
|
49
|
+
targetCrop
|
|
50
50
|
}: {
|
|
51
51
|
rawImage: File;
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
downsizingTo?: { height: number; width: number };
|
|
53
|
+
targetCrop?: `${number}x${number}`;
|
|
54
54
|
}): Promise<File> {
|
|
55
55
|
// Create image element from file
|
|
56
56
|
const img = new Image();
|
|
@@ -62,43 +62,54 @@ async function resizeAndCropImage({
|
|
|
62
62
|
img.src = imageUrl;
|
|
63
63
|
});
|
|
64
64
|
|
|
65
|
-
//
|
|
66
|
-
const [aspectWidth, aspectHeight] = targetAspect.split('x').map(Number);
|
|
67
|
-
const targetRatio = aspectWidth / aspectHeight;
|
|
68
|
-
|
|
69
|
-
// Target dimensions
|
|
70
|
-
const targetWidth = downsize.width;
|
|
71
|
-
const targetHeight = downsize.height;
|
|
72
|
-
|
|
73
|
-
// Calculate source image ratio
|
|
74
|
-
const imgRatio = img.width / img.height;
|
|
75
|
-
|
|
65
|
+
// Step 1: Calculate the dimensions after optional cropping
|
|
76
66
|
let sourceX = 0;
|
|
77
67
|
let sourceY = 0;
|
|
78
68
|
let sourceWidth = img.width;
|
|
79
69
|
let sourceHeight = img.height;
|
|
80
70
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
|
|
71
|
+
if (targetCrop) {
|
|
72
|
+
// Parse target aspect ratio (e.g., "2x3" -> 2/3)
|
|
73
|
+
const [aspectWidth, aspectHeight] = targetCrop.split('x').map(Number);
|
|
74
|
+
const targetRatio = aspectWidth / aspectHeight;
|
|
75
|
+
const imgRatio = img.width / img.height;
|
|
76
|
+
|
|
77
|
+
// Center crop to target ratio
|
|
78
|
+
if (imgRatio > targetRatio) {
|
|
79
|
+
// Image is wider - crop width
|
|
80
|
+
sourceWidth = img.height * targetRatio;
|
|
81
|
+
sourceX = (img.width - sourceWidth) / 2;
|
|
82
|
+
} else if (imgRatio < targetRatio) {
|
|
83
|
+
// Image is taller - crop height
|
|
84
|
+
sourceHeight = img.width / targetRatio;
|
|
85
|
+
sourceY = (img.height - sourceHeight) / 2;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Step 2: Calculate canvas dimensions after optional downsizing
|
|
90
|
+
let canvasWidth = sourceWidth;
|
|
91
|
+
let canvasHeight = sourceHeight;
|
|
92
|
+
|
|
93
|
+
if (downsizingTo) {
|
|
94
|
+
// Scale down to fit within bounds while maintaining aspect ratio
|
|
95
|
+
const scaleX = downsizingTo.width / sourceWidth;
|
|
96
|
+
const scaleY = downsizingTo.height / sourceHeight;
|
|
97
|
+
const scale = Math.min(scaleX, scaleY, 1); // Only downsize, never upsize
|
|
98
|
+
|
|
99
|
+
canvasWidth = sourceWidth * scale;
|
|
100
|
+
canvasHeight = sourceHeight * scale;
|
|
90
101
|
}
|
|
91
102
|
|
|
92
|
-
// Draw
|
|
103
|
+
// Draw image on canvas (no stretching, fills entire canvas)
|
|
93
104
|
const canvas = document.createElement('canvas');
|
|
94
|
-
canvas.width =
|
|
95
|
-
canvas.height =
|
|
105
|
+
canvas.width = canvasWidth;
|
|
106
|
+
canvas.height = canvasHeight;
|
|
96
107
|
const ctx = canvas.getContext('2d')!;
|
|
97
108
|
|
|
98
109
|
ctx.drawImage(
|
|
99
110
|
img,
|
|
100
111
|
sourceX, sourceY, sourceWidth, sourceHeight,
|
|
101
|
-
0, 0,
|
|
112
|
+
0, 0, canvasWidth, canvasHeight
|
|
102
113
|
);
|
|
103
114
|
|
|
104
115
|
// Clean up
|