mosch / react-avatar-editor

Small avatar & profile picture component. Resize and crop uploaded images using a intuitive user interface.
https://react-avatar-editor.netlify.app/
MIT License
2.37k stars 369 forks source link

.jpg images increasing in file size after upload #388

Open hrorich opened 1 year ago

hrorich commented 1 year ago

Using the utility, when we upload a .jpg image, the utility is saving it at 3-5x the original size. We uploaded a .jpg of file size 51kb, and the utility saved it as a 132kb jpeg. We uploaded a .jpg of 1MB, and the utility saved it as a 6.3MB jpeg.

m-a-h1 commented 1 year ago

i have the same issue. i select a file with size 900kB, but when i get the blob format of canvas it became 10MB. the issue is that the canvas with and height is too large like 5000. i don't know how to fix it.

dos41gw commented 1 day ago

This is because its always by default return PNG image, which is naturally bigger in size, then jpeg. To get jpeg use like

const canvas = editorRef.current.getImageScaledToCanvas();
const dataUrl = canvas.toDataURL('image/jpeg', 0.9); 
const blob = await fetch(dataUrl).then((res) => res.blob());
      const formData = new FormData();
      formData.append('avatar', blob, 'avatar.jpg');