Skip to content

Instantly share code, notes, and snippets.

@JosephNC
Last active April 25, 2018 20:47
Show Gist options
  • Save JosephNC/2ba8dbe2849f4c9004331c896d92c932 to your computer and use it in GitHub Desktop.
Save JosephNC/2ba8dbe2849f4c9004331c896d92c932 to your computer and use it in GitHub Desktop.
Function to save base64 image to png in PHP
<?php
function base64_png($data, $filename) {
$img = str_replace('data:image/png;base64,', '', $data);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
return file_put_contents($filename, $data);
}
$filename = __DIR__ . '/' . uniqid() . '.png';
$save = base64_png($_POST['data'], $filename);
echo $save ? 'File saved!' : 'Unable to save file.';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment