duanliang1999 2016-01-13 09:30
浏览 124

无法在服务器上将HTML5 Canvas另存为Image

I know that a similar question has been asked before but none of them helped me. up vote

I have written some JavaScript that allows the user to draw on the canvas in different colors and pen sizes. I want to upload the canvas to my server

This is my index.html:

    <html>
  <head>
  </head>
  <body>
     <canvas id="canvas"></canvas>
     <button id="button_clear" onclick="test()">Clear/upload canvas</button>

<script>
function test(){
var canvas  = document.getElementById("canvas");
var dataURL = canvas.toDataURL();
$.ajax({
  type: "POST",
  url: "script.php",
  data: {
     imgBase64: dataURL
  }
}).done(function(o) {
  console.log('saved');
});
}
</script>

script.php:

<?php
    // requires php5
    //define('UPLOAD_DIR', 'FBdraw/');
    $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);
    print $success ? $file : 'Unable to save the file.';

?>

When I refresh the script.php page it saves a empty 0 kB image.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何实验stm32主通道和互补通道独立输出
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题