diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2021-11-12 02:02:25 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2021-11-12 02:02:25 +0900 |
| commit | 0e4a111f81cceed275d9bec2695f6e401fb654d8 (patch) | |
| tree | 40874799472fa07416f17b50a398ac33b7771905 /src/services/drive/image-processor.ts | |
| parent | update deps (diff) | |
| download | misskey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.gz misskey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.bz2 misskey-0e4a111f81cceed275d9bec2695f6e401fb654d8.zip | |
refactoring
Resolve #7779
Diffstat (limited to 'src/services/drive/image-processor.ts')
| -rw-r--r-- | src/services/drive/image-processor.ts | 107 |
1 files changed, 0 insertions, 107 deletions
diff --git a/src/services/drive/image-processor.ts b/src/services/drive/image-processor.ts deleted file mode 100644 index 493bf5c1cc..0000000000 --- a/src/services/drive/image-processor.ts +++ /dev/null @@ -1,107 +0,0 @@ -import * as sharp from 'sharp'; - -export type IImage = { - data: Buffer; - ext: string | null; - type: string; -}; - -/** - * Convert to JPEG - * with resize, remove metadata, resolve orientation, stop animation - */ -export async function convertToJpeg(path: string, width: number, height: number): Promise<IImage> { - return convertSharpToJpeg(await sharp(path), width, height); -} - -export async function convertSharpToJpeg(sharp: sharp.Sharp, width: number, height: number): Promise<IImage> { - const data = await sharp - .resize(width, height, { - fit: 'inside', - withoutEnlargement: true - }) - .rotate() - .jpeg({ - quality: 85, - progressive: true - }) - .toBuffer(); - - return { - data, - ext: 'jpg', - type: 'image/jpeg' - }; -} - -/** - * Convert to WebP - * with resize, remove metadata, resolve orientation, stop animation - */ -export async function convertToWebp(path: string, width: number, height: number): Promise<IImage> { - return convertSharpToWebp(await sharp(path), width, height); -} - -export async function convertSharpToWebp(sharp: sharp.Sharp, width: number, height: number): Promise<IImage> { - const data = await sharp - .resize(width, height, { - fit: 'inside', - withoutEnlargement: true - }) - .rotate() - .webp({ - quality: 85 - }) - .toBuffer(); - - return { - data, - ext: 'webp', - type: 'image/webp' - }; -} - -/** - * Convert to PNG - * with resize, remove metadata, resolve orientation, stop animation - */ -export async function convertToPng(path: string, width: number, height: number): Promise<IImage> { - return convertSharpToPng(await sharp(path), width, height); -} - -export async function convertSharpToPng(sharp: sharp.Sharp, width: number, height: number): Promise<IImage> { - const data = await sharp - .resize(width, height, { - fit: 'inside', - withoutEnlargement: true - }) - .rotate() - .png() - .toBuffer(); - - return { - data, - ext: 'png', - type: 'image/png' - }; -} - -/** - * Convert to PNG or JPEG - * with resize, remove metadata, resolve orientation, stop animation - */ -export async function convertToPngOrJpeg(path: string, width: number, height: number): Promise<IImage> { - return convertSharpToPngOrJpeg(await sharp(path), width, height); -} - -export async function convertSharpToPngOrJpeg(sharp: sharp.Sharp, width: number, height: number): Promise<IImage> { - const stats = await sharp.stats(); - const metadata = await sharp.metadata(); - - // 不透明で300x300pxの範囲を超えていればJPEG - if (stats.isOpaque && ((metadata.width && metadata.width >= 300) || (metadata.height && metadata!.height >= 300))) { - return await convertSharpToJpeg(sharp, width, height); - } else { - return await convertSharpToPng(sharp, width, height); - } -} |