2022-02-27 11:07:39 +09:00
|
|
|
import sharp from 'sharp';
|
2019-02-05 03:01:36 +09:00
|
|
|
|
|
|
|
|
export type IImage = {
|
|
|
|
|
data: Buffer;
|
2019-04-13 01:43:22 +09:00
|
|
|
ext: string | null;
|
2019-02-05 03:01:36 +09:00
|
|
|
type: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert to JPEG
|
|
|
|
|
* with resize, remove metadata, resolve orientation, stop animation
|
|
|
|
|
*/
|
2019-05-15 21:27:20 +09:00
|
|
|
export async function convertToJpeg(path: string, width: number, height: number): Promise<IImage> {
|
2020-02-14 11:40:45 +09:00
|
|
|
return convertSharpToJpeg(await sharp(path), width, height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function convertSharpToJpeg(sharp: sharp.Sharp, width: number, height: number): Promise<IImage> {
|
|
|
|
|
const data = await sharp
|
2019-02-05 03:01:36 +09:00
|
|
|
.resize(width, height, {
|
|
|
|
|
fit: 'inside',
|
2021-12-09 23:58:30 +09:00
|
|
|
withoutEnlargement: true,
|
2019-02-05 03:01:36 +09:00
|
|
|
})
|
|
|
|
|
.rotate()
|
|
|
|
|
.jpeg({
|
|
|
|
|
quality: 85,
|
2021-12-09 23:58:30 +09:00
|
|
|
progressive: true,
|
2019-02-05 03:01:36 +09:00
|
|
|
})
|
|
|
|
|
.toBuffer();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data,
|
|
|
|
|
ext: 'jpg',
|
2021-12-09 23:58:30 +09:00
|
|
|
type: 'image/jpeg',
|
2019-02-05 03:01:36 +09:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert to WebP
|
|
|
|
|
* with resize, remove metadata, resolve orientation, stop animation
|
|
|
|
|
*/
|
2022-04-28 11:14:03 +09:00
|
|
|
export async function convertToWebp(path: string, width: number, height: number, quality: number = 85): Promise<IImage> {
|
|
|
|
|
return convertSharpToWebp(await sharp(path), width, height, quality);
|
2020-02-14 11:40:45 +09:00
|
|
|
}
|
|
|
|
|
|
2022-04-28 11:14:03 +09:00
|
|
|
export async function convertSharpToWebp(sharp: sharp.Sharp, width: number, height: number, quality: number = 85): Promise<IImage> {
|
2020-02-14 11:40:45 +09:00
|
|
|
const data = await sharp
|
2019-02-05 03:01:36 +09:00
|
|
|
.resize(width, height, {
|
|
|
|
|
fit: 'inside',
|
2021-12-09 23:58:30 +09:00
|
|
|
withoutEnlargement: true,
|
2019-02-05 03:01:36 +09:00
|
|
|
})
|
|
|
|
|
.rotate()
|
|
|
|
|
.webp({
|
2022-04-28 11:14:03 +09:00
|
|
|
quality,
|
2019-02-05 03:01:36 +09:00
|
|
|
})
|
|
|
|
|
.toBuffer();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data,
|
|
|
|
|
ext: 'webp',
|
2021-12-09 23:58:30 +09:00
|
|
|
type: 'image/webp',
|
2019-02-05 03:01:36 +09:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert to PNG
|
|
|
|
|
* with resize, remove metadata, resolve orientation, stop animation
|
|
|
|
|
*/
|
2019-05-15 21:27:20 +09:00
|
|
|
export async function convertToPng(path: string, width: number, height: number): Promise<IImage> {
|
2020-02-14 11:40:45 +09:00
|
|
|
return convertSharpToPng(await sharp(path), width, height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function convertSharpToPng(sharp: sharp.Sharp, width: number, height: number): Promise<IImage> {
|
|
|
|
|
const data = await sharp
|
2019-02-05 03:01:36 +09:00
|
|
|
.resize(width, height, {
|
|
|
|
|
fit: 'inside',
|
2021-12-09 23:58:30 +09:00
|
|
|
withoutEnlargement: true,
|
2019-02-05 03:01:36 +09:00
|
|
|
})
|
|
|
|
|
.rotate()
|
|
|
|
|
.png()
|
|
|
|
|
.toBuffer();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data,
|
|
|
|
|
ext: 'png',
|
2021-12-09 23:58:30 +09:00
|
|
|
type: 'image/png',
|
2019-02-05 03:01:36 +09:00
|
|
|
};
|
|
|
|
|
}
|