2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:50:11 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-12-18 07:40:38 +01:00
|
|
|
import isAnimated from 'is-file-animated';
|
2023-12-24 08:16:58 +01:00
|
|
|
import { isWebpSupported } from './isWebpSupported.js';
|
2023-12-31 01:45:35 +01:00
|
|
|
import type { BrowserImageResizerConfigWithConvertedOutput } from '@misskey-dev/browser-image-resizer';
|
2022-12-18 07:40:38 +01:00
|
|
|
|
|
|
|
const compressTypeMap = {
|
2023-07-07 06:44:31 +02:00
|
|
|
'image/jpeg': { quality: 0.90, mimeType: 'image/webp' },
|
|
|
|
'image/png': { quality: 1, mimeType: 'image/webp' },
|
|
|
|
'image/webp': { quality: 0.90, mimeType: 'image/webp' },
|
|
|
|
'image/svg+xml': { quality: 1, mimeType: 'image/webp' },
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
const compressTypeMapFallback = {
|
2022-12-18 07:40:38 +01:00
|
|
|
'image/jpeg': { quality: 0.85, mimeType: 'image/jpeg' },
|
|
|
|
'image/png': { quality: 1, mimeType: 'image/png' },
|
|
|
|
'image/webp': { quality: 0.85, mimeType: 'image/jpeg' },
|
|
|
|
'image/svg+xml': { quality: 1, mimeType: 'image/png' },
|
|
|
|
} as const;
|
|
|
|
|
2023-12-31 01:45:35 +01:00
|
|
|
export async function getCompressionConfig(file: File): Promise<BrowserImageResizerConfigWithConvertedOutput | undefined> {
|
2023-07-07 06:44:31 +02:00
|
|
|
const imgConfig = (isWebpSupported() ? compressTypeMap : compressTypeMapFallback)[file.type];
|
2022-12-18 07:40:38 +01:00
|
|
|
if (!imgConfig || await isAnimated(file)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
maxWidth: 2048,
|
|
|
|
maxHeight: 2048,
|
|
|
|
debug: true,
|
|
|
|
...imgConfig,
|
|
|
|
};
|
|
|
|
}
|