从html2canvas保存图像

时间:2014-05-26 18:43:13

标签: javascript php jquery html2canvas

我想保存从html2canvas中获取的图像 这是代码,但这样做不起作用 此代码采用div mydiv 的屏幕截图,然后保存。

 <html>
 <head>
 <script src="jquery.js" ></script>
 <script src="html2canvas.js" ></script>
 <script src="jquery.plugin.html2canvas.js" ></script>
 <script>
 $(document).ready(function(){
 $('button').click(function(){
 elem = $('#mydiv');
 html2canvas(elem, {

    onrendered: function(canvas) {

        var img = canvas.toDataURL("image/png");
        var output = img.replace(/^data:image\/(png|jpg);base64,/, "");
        var output = encodeURIComponent(img);
    var cur_path = 'myfolder';

        var Parameters = "image=" + output + "&filedir=" + cur_path;
        $.ajax({
            type: "POST",
            url: "savePNG.php",
            data: Parameters
        });

    }
});
});
});

</script>
</head>
<body>
 <div id="mydiv">
<h1>Here is some content</h1>
</div>
<button>click</button>
</body>
</html>   

savePNG.php

<?php
$image = $_POST['image'];
$filedir = $_POST['filedir'];
$name = time();



$image = str_replace('data:image/png;base64,', '', $image);
$decoded = base64_decode($image);

file_put_contents($filedir . "/" . $name . ".png", $decoded, LOCK_EX);

echo $image;
?>    

这不行。该代码有什么问题。请帮帮我。

0 个答案:

没有答案