1

I want to save the image that is taken from the html2canvas.
Here is code but this doen't work.
This code take the screen shot of the div mydiv and then save it.

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

This is not work. what is the problem with that code. please help me.

Axeem
  • 49
  • 3
  • 8
  • What is it that doesn't work? Is the file created? Does it contain the wrong thing? – WizKid May 26 '14 at 18:46
  • Are there any errors? Have you attempted to debug it in any way? Right now it just looks like you've just copied and pasted most of the code from here: http://stackoverflow.com/questions/17672020/html2canvas-save-image-doesnt-work then posted a question without trying anything yourself. This answer would be a good starting point: http://stackoverflow.com/a/6575502/2287470 – Joe May 26 '14 at 18:47
  • No error and nor create any file in folder. – Axeem May 26 '14 at 18:51
  • Have you checked your browser's console for errors? Is the ajax request successful? – Joe May 26 '14 at 18:52
  • there is a syntax error, Now I removed in my code.And now this is work. – Axeem May 26 '14 at 19:06

0 Answers0