enhance(client): Compress non-animated PNG files (#9334)
* style: fix TS lint errors about `ev.target` * enhance: compress non-animated PNG * PNG to PNG? * defer jest things (add it later) * Delete jest.config.cjs * check the compressed file size * log compression stats * use ?? * handle if ($i == null) Co-authored-by: tamaina <tamaina@hotmail.co.jp>
This commit is contained in:
parent
b6995f6e4b
commit
a47d172d60
4 changed files with 55 additions and 24 deletions
23
packages/client/src/scripts/upload/compress-config.ts
Normal file
23
packages/client/src/scripts/upload/compress-config.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import isAnimated from 'is-file-animated';
|
||||
import type { BrowserImageResizerConfig } from 'browser-image-resizer';
|
||||
|
||||
const compressTypeMap = {
|
||||
'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;
|
||||
|
||||
export async function getCompressionConfig(file: File): Promise<BrowserImageResizerConfig | undefined> {
|
||||
const imgConfig = compressTypeMap[file.type];
|
||||
if (!imgConfig || await isAnimated(file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return {
|
||||
maxWidth: 2048,
|
||||
maxHeight: 2048,
|
||||
debug: true,
|
||||
...imgConfig,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue