diff options
| author | MeiMei <30769358+mei23@users.noreply.github.com> | 2019-02-05 03:01:36 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2019-02-05 03:01:36 +0900 |
| commit | f014b7ae0ece886ef0cff2366b9925e23b34ba6f (patch) | |
| tree | c35760467278da408348fcd459415af7a63e08e9 /src/services/drive/image-processor.ts | |
| parent | Refactor: Better type definition (diff) | |
| download | sharkey-f014b7ae0ece886ef0cff2366b9925e23b34ba6f.tar.gz sharkey-f014b7ae0ece886ef0cff2366b9925e23b34ba6f.tar.bz2 sharkey-f014b7ae0ece886ef0cff2366b9925e23b34ba6f.zip | |
アニメーションを自動再生しないオプション (#4131)
* Refactor
* settings
* Media Proxy
* Replace API response
Diffstat (limited to 'src/services/drive/image-processor.ts')
| -rw-r--r-- | src/services/drive/image-processor.ts | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/services/drive/image-processor.ts b/src/services/drive/image-processor.ts new file mode 100644 index 0000000000..3c538390b0 --- /dev/null +++ b/src/services/drive/image-processor.ts @@ -0,0 +1,75 @@ +import * as sharp from 'sharp'; + +export type IImage = { + data: Buffer; + ext: string; + 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> { + const data = await sharp(path) + .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> { + const data = await sharp(path) + .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> { + const data = await sharp(path) + .resize(width, height, { + fit: 'inside', + withoutEnlargement: true + }) + .rotate() + .png() + .toBuffer(); + + return { + data, + ext: 'png', + type: 'image/png' + }; +} |