-
-
Save parzibyte/a9dad0d2de26eff89e5513234c220ccb 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
<?php | |
/* | |
Tomar una fotografía y guardarla en un archivo | |
@date 2017-11-22 | |
@author parzibyte | |
@web parzibyte.me/blog | |
*/ | |
$imagenCodificada = file_get_contents("php://input"); //Obtener la imagen | |
if(strlen($imagenCodificada) <= 0) exit("No se recibió ninguna imagen"); | |
//La imagen traerá al inicio data:image/png;base64, cosa que debemos remover | |
$imagenCodificadaLimpia = str_replace("data:image/png;base64,", "", urldecode($imagenCodificada)); | |
//Venía en base64 pero sólo la codificamos así para que viajara por la red, ahora la decodificamos y | |
//todo el contenido lo guardamos en un archivo | |
$imagenDecodificada = base64_decode($imagenCodificadaLimpia); | |
//Calcular un nombre único | |
$nombreImagenGuardada = "foto_" . uniqid() . ".png"; | |
//Escribir el archivo | |
file_put_contents($nombreImagenGuardada, $imagenDecodificada); | |
//Terminar y regresar el nombre de la foto | |
exit($nombreImagenGuardada); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment