export class DocImage {
id:string
width:number
height:number
blob:Blob
imageType:string
zipUri:string
constructor(
id:string,
width:number,
height:number,
blob:Blob,
imageType:string,
){
this.id = id
this.width = width ?? 1000
this.height = height ?? 1000
this.blob = blob
this.imageType = imageType
this.zipUri = 'Pictures/' + this.id + '.' + this.imageType.toLocaleLowerCase()
}
getODTValue(): string {
return `
`
}
getODTStyle(): string {
return `
`
}
getManifestEntry() {
return ` `
}
static async fromBlob(imageBlob:Blob): Promise {
const data = await this.getImageDetails(imageBlob)
const imageId = new Date().getMilliseconds().toString() +
new Date().getDate().toString() +
new Date().getMinutes().toString() +
new Date().getSeconds().toString()
return new DocImage(imageId, data.width, data.height, imageBlob, data.extension)
}
async getImageBase64(): Promise{
return new Promise((resolve, reject) => {
const reader = new FileReader();
// Manejar el evento onload para obtener el resultado en Base64
reader.onloadend = () => {
const base64String = reader.result as string;
resolve(base64String);
};
// Manejar errores al leer el Blob
reader.onerror = () => reject(new Error("No se pudo convertir el Blob a Base64"));
// Leer el Blob como una cadena Base64
reader.readAsDataURL(this.blob);
});
}
private static async getImageDetails(blob: Blob) {
return new Promise<{ width: number; height: number; extension: string }>((resolve, reject) => {
// Crear un URL para el Blob
const url = URL.createObjectURL(blob);
// Crear un elemento Image
const img = new Image();
// Manejar el evento onload para obtener el ancho y alto
img.onload = () => {
const width = img.width;
const height = img.height;
// Obtener la extensión del archivo a partir del tipo MIME
const mimeType = blob.type;
const extension = mimeType.split('/')[1];
// Liberar el URL creado
URL.revokeObjectURL(url);
// Resolver la promesa con los detalles de la imagen
resolve({ width, height, extension });
};
// Manejar errores al cargar la imagen
img.onerror = () => {
URL.revokeObjectURL(url);
reject(new Error('No se pudo cargar la imagen'));
};
// Asignar el URL al src de la imagen
img.src = url;
});
}
}