Skip to content

Instantly share code, notes, and snippets.

@dev-armando
Created November 2, 2021 04:57
Show Gist options
  • Save dev-armando/12c9653902695389f3a5526d734d310a to your computer and use it in GitHub Desktop.
Save dev-armando/12c9653902695389f3a5526d734d310a to your computer and use it in GitHub Desktop.
Transformar imagen base64 a un archivo
<?php
function fromBase64ToImg($imgBase64 , $dir , $name_file ){
$pattern = '/data:image\/(.+);base64,(.*)/';
preg_match($pattern, $imgBase64, $matches);
$imageExtension = $matches[1];
$encodedImageData = $matches[2];
$decodedImageData = base64_decode($encodedImageData);
$name = "{$name_file}.{$imageExtension}";
file_put_contents("{$dir}/{$name}", $decodedImageData);
return "{$name}.{$imageExtension}";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment