imageUtils.ts 767 B

1234567891011121314151617181920212223242526
  1. /**
  2. * Manipulation des images
  3. */
  4. class ImageUtils {
  5. /**
  6. * Returns a blob with the given data and the image filetype
  7. *
  8. * @param data
  9. * @param filetype
  10. */
  11. public static newBlob(data: string, filetype: string = 'image/jpeg'): Blob {
  12. return new Blob([data as BlobPart], {type: filetype});
  13. }
  14. /**
  15. * Transforme un Blob en Base64
  16. * @param {Blob} blob
  17. */
  18. public static async blobToBase64(blob: Blob): Promise<string> {
  19. // /!\ Attention: lors de tests unitaires, 'blob.text' plantera si utilisée en même temps que vi.useFakeTimers()
  20. const content = Buffer.from(await blob.text()).toString('base64');
  21. return `data:${blob.type};base64,${content}`
  22. }
  23. }
  24. export default ImageUtils