Save image of canvas on server

27 February 2018 21:09
I'm trying to save, on a folder in my server, the image of all my canvas, using ajax (after click on a button):

$( ".save" ).click(function() {
var c = document.getElementById('canvasID');
var dataURL = c.toDataURL("image/jpeg");
type: "POST",
url: "save-image.php",
data: {
imgBase64: dataURL

where save-image.php is:

define('UPLOAD_DIR', 'images-canvas/');
$img = $_POST['imgBase64'];
$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);

but the image saved is always black. Why? Anyone can help me, please?

Thank you!
Please register or log in to leave a reply.