我正在开发一个项目,其中一个要求是截取网页截图并将其作为图像存储在本地驱动器中。
我浏览了 a blog ,了解了如何使用JavaScript获取屏幕截图。 我能够获得截图,但如何将其保存为本地驱动器中的图像?
我的JQuery代码:
(function (exports) {
function urlsToAbsolute(nodeList) {
if (!nodeList.length) {
return [];
}
var attrName = 'href';
if (nodeList[0].__proto__ === HTMLImageElement.prototype
|| nodeList[0].__proto__ === HTMLScriptElement.prototype) {
attrName = 'src';
}
nodeList = [].map.call(nodeList, function (el, i) {
var attr = el.getAttribute(attrName);
if (!attr) {
return;
}
var absURL = /^(https?|data):/i.test(attr);
if (absURL) {
return el;
} else {
return el;
}
});
return nodeList;
}
function screenshotPage() {
urlsToAbsolute(document.images);
urlsToAbsolute(document.querySelectorAll("link[rel='stylesheet']"));
var screenshot = document.documentElement.cloneNode(true);
var b = document.createElement('base');
b.href = document.location.protocol + '//' + location.host;
var head = screenshot.querySelector('head');
head.insertBefore(b, head.firstChild);
screenshot.style.pointerEvents = 'none';
screenshot.style.overflow = 'hidden';
screenshot.style.webkitUserSelect = 'none';
screenshot.style.mozUserSelect = 'none';
screenshot.style.msUserSelect = 'none';
screenshot.style.oUserSelect = 'none';
screenshot.style.userSelect = 'none';
screenshot.dataset.scrollX = window.scrollX;
screenshot.dataset.scrollY = window.scrollY;
var script = document.createElement('script');
script.textContent = '(' + addOnPageLoad_.toString() + ')();';
screenshot.querySelector('body').appendChild(script);
var blob = new Blob([screenshot.outerHTML], {
type: 'text/html'
});
return blob;
}
function addOnPageLoad_() {
window.addEventListener('DOMContentLoaded', function (e) {
var scrollX = document.documentElement.dataset.scrollX || 0;
var scrollY = document.documentElement.dataset.scrollY || 0;
window.scrollTo(scrollX, scrollY);
});
}
function generate() {
window.URL = window.URL || window.webkitURL;
window.open(window.URL.createObjectURL(screenshotPage()));
}
exports.screenshotPage = screenshotPage;
exports.generate = generate;
})(window);
如我的小提琴所示,我想向用户提供一个按钮/链接,以便用户可以拍摄当前窗口/网页的快照。并能够将该快照下载/保存为图像(png,jpeg,jpg任何格式)。
答案 0 :(得分:1)
您无法直接访问客户端的文件系统。但是,您可以提示他们下载文件。
数据网址
数据URL是链接包含文件内容的URL。例如:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAioAAAO0CAYAAACLKx00
该网址已被裁剪,因此无法实际运作。
这是一个小提琴:http://jsfiddle.net/x876K/6/
您可以使用以下链接强制下载:
<a download="file.png" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAioAAAO0CAYAAACLKx00">text</a>
尝试阅读:
如果我误解了,请告诉我。
画布和图片
您也可以写入画布,并允许用户右键单击并另存为。
答案 1 :(得分:1)
以下是我的回答:
我的index.php
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="js/html2canvas.js"></script>
<script type="text/javascript" src="js/jquery.plugin.html2canvas.js"></script>
<form method="POST" enctype="multipart/form-data" action="save.php" id="myForm">
<input type="hidden" name="img_val" id="img_val" value="" />
</form>
<input type="submit" value="Take Screenshot Of Div Below" onclick="capture();" />
<div id="target">
DIV TO TAKE PHOTO OF.
</div>
<script>
function capture() {
$('#target').html2canvas({
onrendered: function (canvas) {
//Set hidden field's value to image data (base-64 string)
$('#img_val').val(canvas.toDataURL("image/png"));
//Submit the form manually
document.getElementById("myForm").submit();
}
});
}
</script>
我的save.php
<?php
//Get the base-64 string from data
$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
//Save the image
file_put_contents('img.png', $unencodedData);
$file = 'img.png';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
?>
我在a blog找到了这个解决方案。
答案 2 :(得分:0)
来自https://retifrav.github.io/blog/2018/07/23/html-js-screenshot/
的示例<head>
<script src="html2canvas.min.js"></script>
</head>
<div>
<a id="a-make" href="#">Make a screenshot</a>
<a id="a-download" href="#" style="display:none;">Download a screenshot</a>
</div>
<div id="main">
<div id="screenshot">
...
</div>
</div>
<script>
function makeScreenshot()
{
html2canvas(document.getElementById("screenshot"), {scale: 2}).then(canvas =>
{
canvas.id = "canvasID";
var main = document.getElementById("main");
while (main.firstChild) { main.removeChild(main.firstChild); }
main.appendChild(canvas);
});
}
document.getElementById("a-make").addEventListener('click', function()
{
document.getElementById("a-make").style.display = "none";
makeScreenshot();
document.getElementById("a-download").style.display = "inline";
}, false);
document.getElementById("a-download").addEventListener('click', function()
{
this.href = document.getElementById("canvasID").toDataURL();
this.download = "canvas-image.png";
}, false);
</script>