Skip to content

Instantly share code, notes, and snippets.

@Theo-Steiner
Created January 23, 2022 09:05
Show Gist options
  • Save Theo-Steiner/8d5f8a6a1c34481e52935940aa8d6dbe to your computer and use it in GitHub Desktop.
Save Theo-Steiner/8d5f8a6a1c34481e52935940aa8d6dbe to your computer and use it in GitHub Desktop.
Returning a base64 encoded image from a Svelte Kit endpoint
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.
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
};
}
@Adimac93
Copy link

Thanks!

@mhm9002
Copy link

mhm9002 commented Jul 13, 2023

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