javascript - PHP 屏幕截图 - 按钮

标签 javascript php html

我有一个保存屏幕截图的按钮,但不幸的是,当您单击时什么也没有发生

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 并选择位置。我使用了互联网上的指南,在我看来它应该有效

最佳答案

您需要拆分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
            });
        }
    });
}

关于javascript - PHP 屏幕截图 - 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38150065/

相关文章:

javascript - 如何不让 Masonry 一次加载所有 div

php - 基于字符串 "1,5,3,7"的 SQL 查询 -> 保持结果顺序

php - 如何获取大括号内的嵌套值

javascript - 如何欺骗 Iframe 它是最顶层的窗口

javascript - 基于单选框禁用/启用输入字段

html - 选择元素作为文本流的一部分

javascript - JQuery 工具提示仅在输入字段焦点上可见

javascript - 使用 javascript 和 php 从数据库获取数据

javascript - 如何按图库的 ID 过滤 div 元素

php - 保持变量在函数之间保持不变