B
B
Bogdan2019-01-23 00:29:08
JavaScript
Bogdan, 2019-01-23 00:29:08

Upload image from canvas to server?

Hello. Please tell me how to transfer images from canvas to server. Interested in exactly the moment, how to get a file to send? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-01-23
@thewind

var canvas = document.getElementById('myCanvas');
var dataURL = canvas.toDataURL();
$.ajax({
  type: "POST",
  url: "script.php",
  data: { 
     imgBase64: dataURL
  }
});

and PHP code
$img = $_POST['imgBase64'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$fileData = base64_decode($img);
$fileName = 'photo.png';
file_put_contents($fileName, $fileData);

PS: Have you been banned by Google?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question