前端发送页面:需要引入两个包 jquery.js 和 html2canvas.js
html2canvas下载传送门:http://html2canvas.hertzen.com/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Title</title>
<script src="html2canvas.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="box" style="width:100px;background:red;margin:100px;">
<h1>截图、截图</h1>
<h1>截图、截图</h1>
<h1>截图、截图</h1>
</div>
<button id="btn">按钮</button>
<script>
$(function () {
$("#btn").click(function () {
var sr = document.getElementById("box");
html2canvas(sr).then(function (canvas) {
document.body.appendChild(canvas);
var html_canvas = canvas.toDataURL();
$.post('server.php', {html_canvas:html_canvas}, function(json){
}, 'json');
});
});
});
</script>
</body>
</html>
php接收页面
<?php
if (isset($_POST['html_canvas'])) {
$order_id = $_POST['order_id'];
$type_id = $_POST['type_id'];
$html_canvas = $_POST['html_canvas'];
$image = base64_decode(substr($html_canvas, 22));
header('Content-Type: image/png');
$filename = $order_id . '1' . $type_id . ".png";
$fp = fopen($filename, 'w');
fwrite($fp, $image);
fclose($fp);
}
?>