Skip to content

Commit

Permalink
feat: webp convert @frontend (misskey-dev#11150)
Browse files Browse the repository at this point in the history
* webp convert @frontend

* 0.85 → 0.90

---------

Co-authored-by: tamaina <[email protected]>
  • Loading branch information
2 people authored and slofp committed Jul 21, 2023
1 parent 349424d commit e6b959b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/frontend/src/scripts/upload/compress-config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import isAnimated from 'is-file-animated';
import { isWebpSupported } from './isWebpSupported';
import type { BrowserImageResizerConfig } from 'browser-image-resizer';

const compressTypeMap = {
'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 = {
'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];
const imgConfig = (isWebpSupported() ? compressTypeMap : compressTypeMapFallback)[file.type];
if (!imgConfig || await isAnimated(file)) {
return;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/frontend/src/scripts/upload/isWebpSupported.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let isWebpSupportedCache: boolean | undefined;
export function isWebpSupported() {
if (isWebpSupportedCache === undefined) {
const canvas = document.createElement('canvas');
canvas.width = 1;
canvas.height = 1;
isWebpSupportedCache = canvas.toDataURL('image/webp').startsWith('data:image/webp');
}
return isWebpSupportedCache;
}

0 comments on commit e6b959b

Please sign in to comment.