Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 22, 2022 17:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/82a8d57c13467b2f4f996778d12267fc to your computer and use it in GitHub Desktop.
Save parzibyte/82a8d57c13467b2f4f996778d12267fc to your computer and use it in GitHub Desktop.
const comprimirImagen = (imagenComoArchivo, porcentajeCalidad) => {
/*
https://parzibyte.me/blog
*/
return new Promise((resolve, reject) => {
const $canvas = document.createElement("canvas");
const imagen = new Image();
imagen.onload = () => {
$canvas.width = imagen.width;
$canvas.height = imagen.height;
$canvas.getContext("2d").drawImage(imagen, 0, 0);
$canvas.toBlob(
(blob) => {
if (blob === null) {
return reject(blob);
} else {
resolve(blob);
}
},
"image/jpeg",
porcentajeCalidad / 100
);
};
imagen.src = URL.createObjectURL(imagenComoArchivo);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment