Created
January 23, 2022 09:05
-
-
Save Theo-Steiner/8d5f8a6a1c34481e52935940aa8d6dbe to your computer and use it in GitHub Desktop.
Returning a base64 encoded image from a Svelte Kit endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to return a base64 encoded image from a Svelte Kit endpoint. | |
This example has the /routes/[slug].png.ts route dynamic but doesn't do anything with the slug. | |
This is where you could do some fancy image generation based on the slug. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function get({params}) { | |
// do some cool image generation with the params | |
const imageData = | |
'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; | |
const image = Buffer.from(imageData, 'base64'); | |
return { | |
'Content-Type': 'image/png', | |
'Content-Length': image.length, | |
body: image | |
}; | |
} |
I need to do this for images saved on $lib/server/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!