-
-
Save parzibyte/99792bdecd72db73d1745b9bea74850e 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
document.addEventListener("DOMContentLoaded", () => { | |
const $btnSubir = document.querySelector("#subir"); | |
const $imagen = document.querySelector("#imagen"); | |
let cropper = new Cropper($imagen, { | |
responsive: false, // <-- Si no se pone en false, la imagen se mueve cuando cambia el tamaño de la ventana | |
}); | |
$btnSubir.onclick = async () => { | |
if (!cropper) { | |
return; | |
} | |
// Obtener el canvas recortado | |
const canvas = cropper.getCroppedCanvas(); | |
// Convertir la imagen a Base64 y ponerlo en el enlace | |
const data = canvas.toDataURL("image/png"); | |
const fd = new FormData(); | |
fd.append("imagen", data); // Se llama "imagen", en PHP lo recuperamos con $_POST["imagen"] | |
const respuestaHttp = await fetch("imagen.php", { | |
method: "POST", | |
body: fd, | |
}); | |
const nombreImagenSubida = await respuestaHttp.json(); | |
console.log("La imagen ha sido enviada y tiene el nombre de: " + nombreImagenSubida); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment