-
-
Save parzibyte/82a8d57c13467b2f4f996778d12267fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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