Last active
April 25, 2018 20:47
-
-
Save JosephNC/2ba8dbe2849f4c9004331c896d92c932 to your computer and use it in GitHub Desktop.
Function to save base64 image to png in PHP
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 | |
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