mirror of
https://github.com/documenso/documenso.git
synced 2026-07-27 10:25:00 +10:00
refactor: extract image-helpers (#2261)
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import sharp from 'sharp';
|
||||
|
||||
export const optimiseAvatar = async (bytes: string) => {
|
||||
return await sharp(Buffer.from(bytes, 'base64'))
|
||||
.resize(512, 512)
|
||||
.toFormat('jpeg', { quality: 75 })
|
||||
.toBuffer();
|
||||
};
|
||||
|
||||
export const loadAvatar = async (bytes: string) => {
|
||||
const content = await sharp(Buffer.from(bytes, 'base64')).toFormat('jpeg').toBuffer();
|
||||
return {
|
||||
contentType: 'image/jpeg',
|
||||
content,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
import sharp from 'sharp';
|
||||
|
||||
export const loadLogo = async (file: Uint8Array) => {
|
||||
const content = await sharp(file).toFormat('png', { quality: 80 }).toBuffer();
|
||||
|
||||
return {
|
||||
contentType: 'image/png',
|
||||
content,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
import sharp from 'sharp';
|
||||
|
||||
export const TARGET_SIZE = 1000;
|
||||
|
||||
type ResizeImageToGeminiImageOptions = {
|
||||
image: Buffer;
|
||||
size?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resize image to 1000x1000 using fill strategy.
|
||||
* Scales to cover the target area and crops any overflow.
|
||||
*/
|
||||
export const resizeImageToGeminiImage = async ({
|
||||
image,
|
||||
size = TARGET_SIZE,
|
||||
}: ResizeImageToGeminiImageOptions) => {
|
||||
return await sharp(image).resize(size, size, { fit: 'fill' }).toBuffer();
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
import sharp from 'sharp';
|
||||
|
||||
export const svgToPng = async (svg: string) => {
|
||||
return await sharp(Buffer.from(svg)).toFormat('png').toBuffer();
|
||||
};
|
||||
Reference in New Issue
Block a user