在php中保存画布但图像文件中没有数据

ana*_*nam 1 php jquery html5 base64 canvas

以下代码我试图在我的机器上保存画布.在文件夹图像文件中创建但它是0kb.

点击下载按钮我在文本和下一个php页面设置值我正在阅读值$_POST['img_name'];.

JQuery ::

$("#Download").click(function() {
    console.log('Download clicked');
    //alert('Download clicked');
    var myval = canvas.toDataURL();
    $('#img_txt').val(myval);
});
Run Code Online (Sandbox Code Playgroud)

php代码::

<?php
$myval = $_POST['img_name'];
echo "string is ".$myval; 
?>

<?php
$data = base64_decode($_POST['img_name']);

/* Name the file anything you want */
$filename = 'my_image.png';

/* Remove 1st line of base64 string */ 
/* Note: replace '$base64' with your own variable */
$img = str_replace('data:image/png;base64,', '', $data);


/* Replace spaces */
$img = str_replace(' ', '+', $img);

/* Decode string */
$data = base64_decode($img);

/* Full path to upload directory with at the end the filename */
$file = 'D:/upload/'.$filename;

/* Save, make sure the directory is writeable! */
file_put_contents($file, $data);
?>
Run Code Online (Sandbox Code Playgroud)

我检查$ _POST ['img_name']我得到base64字符串?我的代码中有什么问题?

小智 5

替换upload.php文件

if(isset($ _ POST ["image"])&&!empty($ _ POST ["image"])){

// get the image data
$data = $_POST['image'];

// remove the prefix
$uri =  substr($data,strpos($data,",")+1);

// create a filename for the new image
$file = md5(uniqid()) . '.png';

// decode the image data and save it to file
file_put_contents($file, base64_decode($uri));

// return the filename
echo $file;
Run Code Online (Sandbox Code Playgroud)

}