我有获取指定 div 选项卡屏幕截图的代码。但是我遇到了将其显示为 base64 格式的 .png 文件的问题
这是我的代码。它有两个单独的文件
1) 下载.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)保存.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,但我无法做到这一点。请参阅代码帮助我找出问题所在。
提前致谢。