【发布时间】:2015-08-20 09:31:24
【问题描述】:
我目前有一个脚本,可以使用 html2canvas 从画布成功创建图像,包括其包含的 Div 背景图像。我还有一个脚本,可以使用 canvas2image 插件将画布作为图像保存到服务器,但背景不显示。
我遇到的问题是,当我尝试将两者结合起来以便可以将 Div bg 和画布作为图像保存到服务器时,没有任何反应,我认为这是由于 canvas2image 插件没有触发。
我拥有两个插件的代码在这里。
function exportAndSaveCanvas() {
html2canvas($("#containingDiv"), {
background:'#fff',
onrendered: function(canvas) {
// var imgData = canvas.toDataURL('image/jpeg'); <--This would create the image needed
//but I replaced with the line below to tie in the two plugins
var screenshot = Canvas2Image.saveAsPNG(canvas, true);
canvas.parentNode.appendChild(screenshot);
screenshot.id = "canvasimage";
data = $('#canvasimage').attr('src');
canvas.parentNode.removeChild(screenshot);
// Send the screenshot to PHP to save it on the server
var url = 'upload/export.php';
$.ajax({
type: "POST",
url: url,
dataType: 'text',
data: {
base64data : data
}
});
}
});
}
上传图片到服务器的export.php代码在这里
<?php
$data = $_REQUEST['base64data'];
//echo $data;
$image = explode('base64,',$data);
file_put_contents('../uploadimg/myImage.jpg', base64_decode($image[1]));
?>
我希望能够结合这两个插件一起工作,并将我的带有 Div 背景的画布保存到服务器,但看起来 canvas2image 插件没有触发。
谢谢!
【问题讨论】:
-
为什么不直接将背景图片加载到画布中?
-
我是画布新手...不知道该怎么做。
标签: javascript jquery html canvas html2canvas