html2canvas save image doesn't work

后端 未结 1 1715
长发绾君心
长发绾君心 2020-12-29 00:24

I\'m rendering a screenshot with html2canvas 0.4.0 and want to save it as image on my webserver.

To do so, I\'ve written the following function:

JavaScript<

相关标签:
1条回答
  • 2020-12-29 00:51

    In case someone stumbles over the same problem, here is how I solved it:

    The problem depended on the fact, that + in URLs is interpreted as an encoded space (like %20) by most servers. So I needed to encode the data first and then send it via POST to my designated PHP function.

    Here is my code:

    JavaScript

    function screenShot(id) {
    
        html2canvas(id, {
            proxy: "https://html2canvas.appspot.com/query",
            onrendered: function(canvas) {
    
                var img = canvas.toDataURL("image/png");
                var output = encodeURIComponent(img);
    
                var Parameters = "image=" + output + "&filedir=" + cur_path;
                $.ajax({
                    type: "POST",
                    url: "inc/savePNG.php",
                    data: Parameters,
                    success : function(data)
                    {
                        console.log("screenshot done");
                    }
                }).done(function() {
                    //$('body').html(data);
                });
    
            }
        });
    }    
    

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

    Cheers!

    0 讨论(0)
提交回复
热议问题