PHP截图 - 按钮

时间:2016-07-01 16:25:16

标签: javascript php html html5

我有一个保存屏幕截图的按钮,但不幸的是当你点击没有任何事情发生时

的JavaScript

function take_screenshot() {
    html2canvas(document.body, {
        onrendered: function(canvas) {
            var img = canvas.toDataURL()
            $.post("save_screenshot.php", {data: img}, function (file) {
                window.location.href =  "save_screenshot.php?file="+ file
            });
        }
    });
}

PHP

<?php
if($_GET['file']) {
    $file=$_GET['file'];
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: image/png');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        unlink($file);
        exit;
    }
}

if($_POST['data']) {
    $data = $_POST['data'];
    $file = md5(uniqid()) . '.png';
    $uri =  substr($data,strpos($data,",")+1);
    file_put_contents('./'.$file, base64_decode($uri));
    echo $file;
    exit();
}

我希望成为一个屏幕保存为png和我选择位置。我在互联网上使用指南,在我看来它应该工作

1 个答案:

答案 0 :(得分:0)

您需要拆分dataUrl,因为它由,分隔

function take_screenshot() {
    html2canvas(document.body, {
        onrendered: function(canvas) {
            var img = canvas.toDataURL()
            $.post("save_screenshot.php", {data: img.split(',').pop()}, function (file) {
                window.location.href =  "save_screenshot.php?file="+ file
            });
        }
    });
}