Skip to content

Commit

Permalink
Compress image on client
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy committed Feb 22, 2024
1 parent e892d40 commit 47af81e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/node": "20.10.5",
"@types/react": "18.2.46",
"@types/react-dom": "18.2.18",
"browser-image-compression": "^2.0.2",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.1",
"cmdk": "^0.2.0",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/components/logic/UploadImage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Intent, intentProperties } from '@/lib/image';
import { cn } from '@/lib/utils';
import { Actor } from '@/schema/resources/actor';
import imageCompression from 'browser-image-compression';
import { Loader2 } from 'lucide-react';
import { useTranslations } from 'next-intl';
import React, {
Expand Down Expand Up @@ -80,8 +81,13 @@ export default function UploadImage({
(async (): Promise<true | void> => {
setIsLoading(true);

const compressed = await imageCompression(blob as File, {
maxSizeMB: 5,
useWebWorker: true,
});

const data = new FormData();
data.append('file', blob, 'profilepicture.png');
data.append('file', compressed, 'profilepicture.png');
data.append('intent', intent);
if (actor?.type != 'user') data.append('target', actor.id);

Expand Down

0 comments on commit 47af81e

Please sign in to comment.