feat: base64ToImage.ts
This commit is contained in:
parent
c07bb959cf
commit
7d78cd1440
1 changed files with 20 additions and 0 deletions
20
frontend/utils/base64ToImage.ts
Normal file
20
frontend/utils/base64ToImage.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
/**
|
||||
* Converts a Base64 string to a valid image source URL.
|
||||
*
|
||||
* @param base64String - The "image" field from the learning path JSON response.
|
||||
* @returns A properly formatted data URL for use in an <img> tag.
|
||||
*
|
||||
* @example
|
||||
* // Fetch the learning path data and extract the image
|
||||
* const response = await fetch( learning path route );
|
||||
* const data = await response.json();
|
||||
* const base64String = data.image;
|
||||
*
|
||||
* // Use in an <img> element
|
||||
* <img :src="convertBase64ToImageSrc(base64String)" alt="Learning Path Image" />
|
||||
*/
|
||||
export function convertBase64ToImageSrc(base64String: string): string {
|
||||
return base64String.startsWith("data:image")
|
||||
? base64String
|
||||
: `data:image/png;base64,${base64String}`;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue