0

I'm trying to save my canvas as an image to my server but it is not working.

javascript

var data = canvas.toDataURL();
var picture = document.getElementById("my-picture");

PHP

$canvas = base64_decode(str_replace('data:image/png;base64,', '' , $value['picture']));
file_put_contents('my_custom_path/my_custom_filename.png', $canvas);

this code was given to me but I am not sure how to implement the php part.

Zach10za
  • 100
  • 8
  • This is an often asked and answered question here on Stackoverflow. You might try a search(!). Here's one example: http://stackoverflow.com/questions/24575090/how-can-we-save-html5-canvas-image-to-database-in-php/24577302#24577302 – markE Aug 10 '14 at 20:54
  • The point of asking is so that I can participate in the discussion because I do not have enough reputation to participate in other posts. The reputation system makes absolutely no sense especially if you're going to flag all my posts. You should consider removing the reputation requirement for commenting. @markE – Zach10za Aug 10 '14 at 21:35
  • I didn't flag your post or downvote you. I just suggest that you will find this question is successfully answered often so you might try the search box in the upper right corner. ;-) – markE Aug 10 '14 at 21:37
  • I understand but I have looked for the question and I have found the posts you are referring to. But I am a novice at javascript/php and the answers from the other posts don't go into enough detail for me. Naturally I would just add a comment on there post but I am unable to due to my lack of reputation. @markE – Zach10za Aug 10 '14 at 21:45

1 Answers1

0

Assuming your Javascript/HTML is correct (make sure you are grabbing the correct POST value) try this PHP code instead:

$data = str_replace('data:image/png;base64,', '', $data);
$data = base64_decode( str_replace(' ', '+', $data) );
$success = file_put_contents('my_custom_path/my_custom_filename.png', $data);

Also make sure the directory that you are placing the file in is accessible.

EvilZebra
  • 1,072
  • 8
  • 18