druybew06513 2014-05-04 03:39
浏览 322
已采纳

当从base64数据字符串保存画布图像服务器端时,它会产生空白图像

I have problem with saving canvas image in PHP. I get a blank .png file. I search a lot about this problem but cant find anything useful about this. Why does it save a blank image instead of rendering real image?

JavaScript code:

html2canvas([document.getElementById('dadycool')], {
  onrendered: function (canvas) {
        var data = canvas.toDataURL();
        var image = new Image();
        image.src = data;
        document.getElementById('imagec').appendChild(image);
        console.log(data);
        $.ajax({
  type: "POST",
  url: "up.php",
  data: { 
     imgBase64: data
  }
}).done(function(o) {
  console.log('saved'); 
}); 
}   

PHP code:

<?php
// requires php5
define('localhost/samp/sample2/uploads', 'images/');
$img = $_POST['imgBase64'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = localhost/samp/sample2/uploads . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
  • 写回答

2条回答 默认 最新

  • dou4064 2014-05-05 10:40
    关注

    I suspect you haven't configured your development box to display PHP error messages. You are defining a constant that is not a valid identifier:

    define('localhost/samp/sample2/uploads', 'images/');
    

    That means that you cannot use it directly:

    $file = localhost/samp/sample2/uploads . uniqid() . '.png';
    

    ... should be triggering:

    Notice: Use of undefined constant localhost - assumed 'localhost'
    Notice: Use of undefined constant samp - assumed 'samp'
    Warning: Division by zero
    Notice: Use of undefined constant sample2
    Warning: Division by zero
    Notice: Use of undefined constant uploads - assumed 'uploads'
    Warning: Division by zero
    

    ... and file will only contain the base file name (e.g. 53676a01cdb59.png) but not path component. You need to use this syntax:

    $file = constant('localhost/samp/sample2/uploads') . uniqid() . '.png';
    

    ... or, even better, give the constant a sensible name:

    define('DIR_UPLOADS', 'images/');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?