我希望能够使用php下载脚本将html2canvas下载到图像文件中。该页面有一个滑动条,其形状供用户选择进行练习。需要捕获包含显示结果的网页并将其保存到图像文件中。
下面是我在html代码中嵌入的脚本。
function triggerPull() {
if (document.getElementById('name').value != "") {
html2canvas(document.getElementById('body'), {
onrendered: function(canvas) {
var dataURL = canvas.toDataURL('img/png');
canvas.src = dataURL;
document.getElementById('url').value = dataURL;
document.download.submit();
}
});
} else {
alert('Please fill in your name first.');
}
}
我为downloader.php文件尝试了下面的脚本。我只得到一个名为downloader.php.png的空白图像文件请帮助!
header('Content-type: image/png');
header('Content-Disposition: attachment; filename="' . $_POST['name'] .'"');
$encoded = $_POST['img'];
$encoded = str_replace(' ', '+', $encoded);
$decoded = base64_decode($encoded);
echo $decoded;
?>
答案 0 :(得分:-1)
试试此代码
$file = $_POST['url'];
$file = str_replace(' ', '+', $file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=chart.png');
header('Content-Transfer-Encoding: binary');
readfile($file);