diff options
| author | MeiMei <30769358+mei23@users.noreply.github.com> | 2020-02-14 11:40:45 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-14 11:40:45 +0900 |
| commit | 439563c5d6cf2ceced1705687b5bcbebf429e12e (patch) | |
| tree | e0335cc2a8245ca852c02c9bd32806851de7baca /src/services/drive/image-processor.ts | |
| parent | Update CHANGELOG.md (diff) | |
| download | sharkey-439563c5d6cf2ceced1705687b5bcbebf429e12e.tar.gz sharkey-439563c5d6cf2ceced1705687b5bcbebf429e12e.tar.bz2 sharkey-439563c5d6cf2ceced1705687b5bcbebf429e12e.zip | |
サムネイルをJPEGで生成するように (#5941)
Diffstat (limited to 'src/services/drive/image-processor.ts')
| -rw-r--r-- | src/services/drive/image-processor.ts | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/services/drive/image-processor.ts b/src/services/drive/image-processor.ts index 21a05fa9e4..493bf5c1cc 100644 --- a/src/services/drive/image-processor.ts +++ b/src/services/drive/image-processor.ts @@ -11,7 +11,11 @@ export type IImage = { * 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) + 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 @@ -35,7 +39,11 @@ export async function convertToJpeg(path: string, width: number, height: number) * 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) + 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 @@ -58,7 +66,11 @@ export async function convertToWebp(path: string, width: number, height: number) * 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) + 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 @@ -73,3 +85,23 @@ export async function convertToPng(path: string, width: number, height: number): 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); + } +} |