Skip to content

Instantly share code, notes, and snippets.

@fazlurr
Forked from xjamundx/canvas-upload.php
Created March 27, 2014 07:20
Show Gist options
  • Save fazlurr/9802071 to your computer and use it in GitHub Desktop.
Save fazlurr/9802071 to your computer and use it in GitHub Desktop.
Function to save base64 image to png with PHP
<?php
// requires php5
define('UPLOAD_DIR', 'images/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
@IngLuisCastro
Copy link

Genial, me funcionó

`

$data = $_POST["image"];	

$rand = uniqid();

$img = $_POST['image'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);

$file = "../firmas/" .  $rand . '.png';

$movefile = file_put_contents($file, $data);


$response = array(
	'movefile' => $movefile, 
	'rand' => $rand,
	'file' => $file
);

echo json_encode($response);

`

@shamsulaman786
Copy link

Thanks @fazlurr , it worked perfectly for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment