mirror of
https://github.com/misskey-dev/media-proxy.git
synced 2025-08-10 02:03:56 +09:00
✌️
This commit is contained in:
45
src/image-processor.ts
Normal file
45
src/image-processor.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import sharp from 'sharp';
|
||||
import { Readable } from 'node:stream';
|
||||
|
||||
export type IImage = {
|
||||
data: Buffer;
|
||||
ext: string | null;
|
||||
type: string;
|
||||
};
|
||||
|
||||
export type IImageStream = {
|
||||
data: Readable;
|
||||
ext: string | null;
|
||||
type: string;
|
||||
};
|
||||
|
||||
export type IImageStreamable = IImage | IImageStream;
|
||||
|
||||
export const webpDefault: sharp.WebpOptions = {
|
||||
quality: 85,
|
||||
alphaQuality: 95,
|
||||
lossless: false,
|
||||
nearLossless: false,
|
||||
smartSubsample: true,
|
||||
mixed: true,
|
||||
};
|
||||
|
||||
export function convertToWebpStream(path: string, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
|
||||
return convertSharpToWebpStream(sharp(path), width, height, options);
|
||||
}
|
||||
|
||||
export function convertSharpToWebpStream(sharp: sharp.Sharp, width: number, height: number, options: sharp.WebpOptions = webpDefault): IImageStream {
|
||||
const data = sharp
|
||||
.resize(width, height, {
|
||||
fit: 'inside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.rotate()
|
||||
.webp(options)
|
||||
|
||||
return {
|
||||
data,
|
||||
ext: 'webp',
|
||||
type: 'image/webp',
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user