Updates
This commit is contained in:
@@ -2,36 +2,38 @@ export type ImageInputToBase64FunctionReturn = {
|
||||
imageBase64?: string;
|
||||
imageBase64Full?: string;
|
||||
imageName?: string;
|
||||
imageType?: string;
|
||||
};
|
||||
|
||||
export type ImageInputToBase64FunctioParam = {
|
||||
imageInput: HTMLInputElement;
|
||||
imageInput?: HTMLInputElement;
|
||||
maxWidth?: number;
|
||||
mimeType?: string;
|
||||
file?: File;
|
||||
};
|
||||
|
||||
export default async function imageInputToBase64({
|
||||
imageInput,
|
||||
maxWidth,
|
||||
mimeType,
|
||||
file,
|
||||
}: ImageInputToBase64FunctioParam): Promise<ImageInputToBase64FunctionReturn> {
|
||||
try {
|
||||
if (!imageInput.files?.[0]) {
|
||||
throw new Error("No Files found in this image input");
|
||||
const finalFile = file || imageInput?.files?.[0];
|
||||
|
||||
if (!finalFile) {
|
||||
throw new Error("No Files found");
|
||||
}
|
||||
let imagePreviewNode = document.querySelector(
|
||||
`[data-imagepreview='image']`
|
||||
);
|
||||
let imageName = imageInput.files[0].name.replace(/\..*/, "");
|
||||
|
||||
let imageName = finalFile.name.replace(/\..*/, "");
|
||||
|
||||
let imageDataBase64: string | undefined;
|
||||
|
||||
const MIME_TYPE = mimeType ? mimeType : "image/jpeg";
|
||||
const MIME_TYPE = mimeType ? mimeType : finalFile.type;
|
||||
const QUALITY = 0.95;
|
||||
const MAX_WIDTH = maxWidth ? maxWidth : null;
|
||||
|
||||
const file = imageInput.files[0];
|
||||
const blobURL = URL.createObjectURL(file);
|
||||
const blobURL = URL.createObjectURL(finalFile);
|
||||
const img = new Image();
|
||||
|
||||
img.src = blobURL;
|
||||
@@ -76,6 +78,7 @@ export default async function imageInputToBase64({
|
||||
imageBase64: imageDataBase64?.replace(/.*?base64,/, ""),
|
||||
imageBase64Full: imageDataBase64,
|
||||
imageName: imageName,
|
||||
imageType: MIME_TYPE,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.log("Image Processing Error! =>", error.message);
|
||||
@@ -87,7 +90,3 @@ export default async function imageInputToBase64({
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
|
||||
Reference in New Issue
Block a user