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条)

报告相同问题?

悬赏问题

  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化