2

这段代码几乎可以工作
,这会将整个页面输出为 jpg
问题:我怎样才能只抓取里面的内容'#myDiv'并将其输出为 jpg 文件?

JS:

$('.myButton').click(function(){
    $('#myDiv').html2canvas();//<< has no effect
    var queue = html2canvas.Parse();
    var canvas = html2canvas.Renderer(queue,{elements:{length:0}});
    var img = canvas.toDataURL();
    img.replace(/^data:image\/(png|jpg);base64,/, "");
    $.post( "postIO.php", {img:img}, function(data) {
        //$('#recieve').append(data);
    }); 
    return false;
});

postIO.php:

$canvasImg = $_POST['img'];    
//$canvasImg = str_replace('data:image/png;base64,', '', $canvasImg);

$data = base64_decode($canvasImg);
$File = "z.jpg"; 
$Handle = fopen($File, 'w');
fwrite($Handle, $data);  
fclose($Handle);

这里参考

4

2 回答 2

3

我已经下载并尝试了 html2canvas,然后我发现 jquery 插件没有完成(它只是捕获图像并创建画布,没有用)所以我自己编写捕获代码。

var el = $('div').get(0);

function saveData(dturl){
    //Upload here
    console.debug(dturl);
}

html2canvas.Preload(el, {
    complete: function(image){
        var queue = html2canvas.Parse(el, image, {elements: el}),
            $canvas = $(html2canvas.Renderer(queue, {elements: el}));
        saveData($canvas[0].toDataURL());
    }
});

希望对你有帮助

所以要与您的程序一起使用,您必须编写

function saveData(dturl){
    dturl.replace(/^data:image\/(png|jpg);base64,/, "");
    $.post( "postIO.php", {img:dturl}, function(data) {
        //$('#recieve').append(data);
    }); 
}

$('.myButton').click(function(){
    var el = $('#myDiv').get(0); 
    html2canvas.Preload(el, {
        complete: function(image){
            var queue = html2canvas.Parse(el, image, {elements: el}),
                $canvas = $(html2canvas.Renderer(queue, {elements: el}));
            saveData($canvas[0].toDataURL());
        }
    });
});
于 2012-05-28T02:20:56.080 回答
1

之后var canvasImg = canvasRecord.toDataURL("image/jpg");,您可能需要使用var data = canvasImg.replace(/^data:image\/(png|jpg);base64,/, "");

那是$canvasImg = $_POST['canvasImg'];不是 $_POST['img']?

于 2012-05-28T02:19:09.293 回答