我将尝试使用html2canvas获取屏幕截图。对于文本来说,它运行良好,但是如果要渲染CDN图像,则无法正常工作。如果我将图像托管在服务器上,则可以正常工作,但是如果尝试从CDN链接加载图像,则这些图像不会呈现。
我的代码如下:
index.php
<div id="target">
<img id="editor_Img_1" src="http://d2j259rizy5u5h.cloudfront.net/outputvideos/thumbs/email_44_2015_03_02_54f462457526c-00001.png" alt="event-video" style="height: 200px; width: 320px;">
</div>
function capture() {
$('#target').html2canvas({
onrendered: function (canvas) {
var img = canvas.toDataURL();
console.log(img);
$('<img>',{src:img}).appendTo($('img'));
//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();
}
});
}
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);
echo $filteredData;
//Save the image
file_put_contents('img.png', $unencodedData);
?>
<h2>Save the image and show to user</h2>
<table>
<tr>
<td>
<a href="img.png" target="blank">
Click Here to See The Image Saved to Server</a>
</td>
<td align="right">
<a href="index.php">
Click Here to Go Back</a>
</td>
</tr>
<tr>
<td colspan="2">
<br />
<br />
<span>
Here is Client-sided image:
</span>
<br />
<?php
//Show the image
echo '<img src="'.$_POST['img_val'].'" />';
?>
</td>
</tr>
</table>