我的代码采用了指定div选项卡的屏幕截图。但我遇到的问题是将其显示为base64格式的.png文件
这是我的代码。 它有两个单独的文件
1)Download.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>
<!-- -->
<div id="target">
<img src="images/1.jpg" alt="Smiley face" width="100" height="100">
</div>
<script type="text/javascript">
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>
<input type="submit" value="Take Screenshot Of Div" onclick="return capture();" />
<form method="POST" enctype="multipart/form-data" action="save.php" id="myForm">
<input type="hidden" name="img_val" id="img_val" value="" />
</form>
2)Save.php
<?php
//Show the image
$data = $_POST['img_val'];
//Get the base-64 string from data
$uri = substr($data, strpos($data, ",") + 1);
//Decode the string
$unencodedData = base64_decode($uri);
$im = str_replace($data, '+', 'img.png');
//Save the image
$v = file_put_contents($data, $im);
$result = '<img src="' . $im . '" />';
print_r($result);
?>
我需要将图像显示为localhost / xyz / screenshot / img.png,我无法做到。请看代码帮我解决这个问题。
提前致谢。
答案 0 :(得分:0)
我无法在您的代码中看到html
中id属性为target
的任何元素,但您正在使用它
$('#target').html2canvas({
类似的问题[这里] [1]
我发现了一些有用的东西
var html2obj = html2canvas($('body'));
var queue = html2obj.parse();
var canvas = html2obj.render(queue);
var img = canvas.toDataURL();
window.open(img);
[1]: http://stac
koverflow.com/questions/10457608/create-screenshot-of-webpage-using-html2canvas-unable-to-initialize-properly