Método para facilitar a conversão de base64 para imagem em php
public function ArmazenarImagem($id, $data){
if (preg_match('/^data:image\/(\w+);base64,/', $data, $type)) {
$data = substr($data, strpos($data, ',') + 1);
$type = strtolower($type[1]); // jpg, png, gif
if (!in_array($type, [ 'jpg', 'jpeg', 'gif', 'png' ])) {
}
$data = str_replace( ' ', '+', $data );
$data = base64_decode($data);
if ($data === false) {
throw new \Exception('base64_decode failed');
}
$path = getcwd() . "/img/{$id}";
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
file_put_contents(getcwd() . "/img/{$id}.{$type}", $data);
} else {
throw new \Exception('did not match data URI with image data');
}
}
No método passo 2 parametros id e o imagem em base64, verifico se tem a extensão de imagem, caso sim, verifico se a pasta existe, se positivo, faço a criação da imagem no formato que foi identificada.