function compressImage(imageBytes, quality) {
  const canvas = document.createElement("canvas");
  const context = canvas.getContext("2d");
  const image = new Image();

  return new Promise((resolve, reject) => {
    image.onload = () => {
      const width = image.width;
      const height = image.height;
      canvas.width = width;
      canvas.height = height;
      context.clearRect(0, 0, width, height);
      context.drawImage(image, 0, 0, width, height);
      canvas.toBlob(
        (blob) => {
          const reader = new FileReader();
          reader.onloadend = () => {
            const compressedBytes = new Uint8List.fromList(reader.result);
            resolve(compressedBytes);
          };
          reader.onerror = reject;
          reader.readAsArrayBuffer(blob);
        },
        "image/jpeg",
        quality / 100
      );
    };

    image.onerror = reject;
    image.src = URL.createObjectURL(
      new Blob([imageBytes], { type: "image/jpeg" })
    );
  });
}
