html2canvas输出选中div PHP [英] html2canvas output selected div PHP

查看:92
本文介绍了html2canvas输出选中div PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码几乎可以正常工作

这会将整个页面输出为jpg

问题:我怎样才能抓取内部的内容'#myDiv'并输出为jpg文件?


$
JS:

this code almost works
this will output the entire page into a jpg
question: how can i grab only the content inside '#myDiv' and output that as a jpg file?

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:

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);

来自 here

推荐答案

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

I have download and try html2canvas, then I find out the jquery plugin does not complete (it's does nothing than capture the image and create canvas, no use) so I write capture code myself.

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());
    }
});

希望它可以帮到你

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

so to use with your program you have to write

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());
        }
    });
});

这篇关于html2canvas输出选中div PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆