initial implementation of placeholder images, thumbnails and blurring between using sharp

This commit is contained in:
2023-07-11 18:47:29 +02:00
parent abecc0e71f
commit 5ea8502caf
133 changed files with 124 additions and 54 deletions

View File

@ -3,18 +3,40 @@ import path from 'path'
import type { RequestHandler } from '@sveltejs/kit';
import { BEARER_TOKEN } from '$env/static/private'
import { error } from '@sveltejs/kit';
import { Image } from '../../../../models/Image';
import { IMAGE_DIR } from '$env/static/private'
import sharp from 'sharp';
export const POST = (async ({ request }) => {
const data = await request.json();
const filePath = path.join(
process.cwd(),
"static",
"images",
data.filename as string
);
const file = data.image;
if(data.bearer === BEARER_TOKEN){
writeFileSync(filePath, file, 'base64');
let full_res = new Buffer.from(data.image, 'base64')
// reduce image size if over 500KB
const MAX_SIZE_KB = 500
//const metadata = await sharp(full_res).metadata()
////reduce image size if larger than 500KB
//if(metadata.size > MAX_SIZE_KB*1000){
// full_res = sharp(full_res).
// webp( { quality: 70})
// .toBuffer()
//}
await sharp(full_res)
.toFormat('webp')
.toFile(path.join(IMAGE_DIR,
"full",
data.name + ".webp"))
await sharp(full_res)
.resize({ width: 800})
.toFormat('webp')
.toFile(path.join(IMAGE_DIR,
"thumb",
data.name + ".webp"))
await sharp(full_res)
.resize({ width: 20})
.toFormat('webp')
.toFile(path.join(IMAGE_DIR,
"placeholder",
data.name + ".webp"))
return new Response(JSON.stringify({msg: "Added image successfully"}),{
status: 200,
});