datasquirel/client/media/imageInputFileToBase64.ts

107 lines
3.3 KiB
TypeScript
Raw Normal View History

2025-01-10 19:10:28 +00:00
import { ImageInputFileToBase64FunctionReturn } from "../../package-shared/types";
type Param = {
imageInputFile: File;
maxWidth?: number;
imagePreviewNode?: HTMLImageElement;
};
2023-09-21 14:00:04 +00:00
/**
2025-01-10 19:10:28 +00:00
* # Image input File top Base64
2023-09-21 14:00:04 +00:00
*/
2025-01-10 19:10:28 +00:00
export default async function imageInputFileToBase64({
2023-12-15 10:08:23 +00:00
imageInputFile,
maxWidth,
imagePreviewNode,
2025-01-10 19:10:28 +00:00
}: Param): Promise<ImageInputFileToBase64FunctionReturn> {
2023-09-21 14:00:04 +00:00
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
try {
let imageName = imageInputFile.name.replace(/\..*/, "");
2025-01-10 19:10:28 +00:00
let imageDataBase64: string | undefined;
let imageSize: number | undefined;
2023-09-21 14:00:04 +00:00
let canvas = document.createElement("canvas");
const MIME_TYPE = imageInputFile.type;
const QUALITY = 0.95;
const MAX_WIDTH = maxWidth ? maxWidth : null;
2025-01-10 19:10:28 +00:00
const file = imageInputFile;
2023-09-21 14:00:04 +00:00
const blobURL = URL.createObjectURL(file);
const img = new Image();
/** ********************* Add source to new image */
img.src = blobURL;
imageDataBase64 = await new Promise((res, rej) => {
/** ********************* Handle Errors in loading image */
img.onerror = function () {
URL.revokeObjectURL(this.src);
console.log("Cannot load image");
};
/** ********************* Handle new image when loaded */
2025-01-10 19:10:28 +00:00
img.onload = function (e) {
const imgEl = e.target as HTMLImageElement;
URL.revokeObjectURL(imgEl.src);
2023-09-21 14:00:04 +00:00
if (MAX_WIDTH) {
const scaleSize = MAX_WIDTH / img.naturalWidth;
2023-12-15 10:08:23 +00:00
canvas.width =
img.naturalWidth < MAX_WIDTH
? img.naturalWidth
: MAX_WIDTH;
canvas.height =
img.naturalWidth < MAX_WIDTH
? img.naturalHeight
: img.naturalHeight * scaleSize;
2023-09-21 14:00:04 +00:00
} else {
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
}
const ctx = canvas.getContext("2d");
2025-01-10 19:10:28 +00:00
ctx?.drawImage(img, 0, 0, canvas.width, canvas.height);
2023-09-21 14:00:04 +00:00
const srcEncoded = canvas.toDataURL(MIME_TYPE, QUALITY);
if (imagePreviewNode) {
imagePreviewNode.src = srcEncoded;
}
res(srcEncoded);
};
});
imageSize = await new Promise((res, rej) => {
canvas.toBlob(
(blob) => {
2025-01-10 19:10:28 +00:00
res(blob?.size);
2023-09-21 14:00:04 +00:00
},
MIME_TYPE,
QUALITY
);
});
return {
2025-01-10 19:10:28 +00:00
imageBase64: imageDataBase64?.replace(/.*?base64,/, ""),
2023-09-21 14:00:04 +00:00
imageBase64Full: imageDataBase64,
imageName: imageName,
imageSize: imageSize,
};
2025-01-10 19:10:28 +00:00
} catch (error: any) {
2023-09-21 14:00:04 +00:00
console.log("Image Processing Error! =>", error.message);
return {
2025-01-10 19:10:28 +00:00
imageBase64: undefined,
imageBase64Full: undefined,
imageName: undefined,
imageSize: undefined,
2023-09-21 14:00:04 +00:00
};
}
2025-01-10 19:10:28 +00:00
}