将“toDataURL”图像保存到服务器



我尝试过实现signature_pad,但老实说我不明白。

如何返回图像值并将其发布到我的服务器?

编辑:

我试过解码:

JavaScript "app.js":

saveButton.addEventListener("click", function (event) {
    if (signaturePad.isEmpty()) {
        alert("Please provide signature first.");
    } else {
        saveSignature(signaturePad.toDataURL());
    }
});
function saveSignature(dataURL) {
$.ajax({
  type: "POST",
  url: "script.php",
  data: { 
     imgBase64: dataURL
  }
}).done(function(o) {
  console.log('saved'); 
});
}

PHP, "script.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.';
?>

在 PHP 代码中,您必须获取 $_POST['imgBase64'] 而不是 $_POST['img']

您的 PHP 代码不返回json数据

试试这个: echo json_encode(['result' => $success ? $file : 'Unable to save the file.'])

最新更新