【问题标题】:html2canvas save image doesn't workhtml2canvas 保存图像不起作用
【发布时间】:2013-07-14 08:48:30
【问题描述】:

我正在使用 html2canvas 0.4.0 渲染屏幕截图,并希望将其保存为我的网络服务器上的图像。

为此,我编写了以下函数:

JavaScript

function screenShot(id) {

    html2canvas(id, {
        proxy: "https://html2canvas.appspot.com/query",
        onrendered: function(canvas) {

            $('body').append(canvas); // This works perfect...

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

            var Parameters = "image=" + output + "&filedir=" + cur_path;
            $.ajax({
                type: "POST",
                url: "inc/saveJPG.php",
                data: Parameters,
                success : function(data)
                {
                    console.log(data);
                }
            }).done(function() {
            });

        }
    });
}    

保存JPG.php

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

    $decoded = base64_decode($image);

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


    echo $name;
?>    

画布呈现后,我可以完美地将其附加到 HTML 正文中,但将其保存在我的服务器上会导致 (?) 文件损坏。

我可以在 IrvanView 中读取尺寸,但图像是透明/空的? 该文件大小约为 2.076 KB。所以它并不是真正的空。

我也尝试过使用 JPEG,但它导致文件损坏,并且 IrfanView 显示类似“虚假标记长度”的内容。

屏幕截图的尺寸为 650x9633。 POST 方法的数据量很大吗?

【问题讨论】:

  • 我会将带有image/png 标头的$decoded 回显到浏览器,看看你是否先得到实际图像
  • 使用echo (int)(str_replace('M', '', ini_get('post_max_size')) * 1024 * 1024);检查最大帖子大小
  • @DanFromGermany 感谢您的回复。我检查了 post_max_size 并将其设置为 64MB(您的线路回显 67108864)。
  • @DevZer0 我通过添加 header('Content-Type: image/png'); 尝试了您的建议到我的 php 并将 $decoded 回显到我的 ajax 函数。 AJAX 函数使用 console.log 输出图像,但在 Firebug 中我得到的只是这样的:�PNG ....任何想法如何调试这个问题?
  • 不要通过ajax返回值,让返回值以弹窗之类的方式返回

标签: php jquery ajax html2canvas


【解决方案1】:

如果有人遇到同样的问题,我是这样解决的:

问题取决于这样一个事实,即大多数服务器将 URL 中的 + 解释为编码空间(如 %20)。所以我需要先对数据进行编码,然后通过POST将其发送到我指定的PHP函数。

这是我的代码:

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

        }
    });
}    

保存PNG.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;
?>    

干杯!

【讨论】:

  • @dschu 非常感谢。请告诉我们代理的需求是什么?
  • @DoulatKhan 当您要渲染的页面包含不在同一个域中的图像时,它们无法被渲染。因此,需要一个代理来加载跨域图像。 From the docs: Url to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.
  • @dschu 代理不再工作,并且出现此错误“Python 2.5 不再可用。请参阅以获取更多信息。”
  • 代理的任何替代方式
猜你喜欢
  • 2019-05-28
  • 2017-11-04
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 2014-11-16
  • 2016-02-13
  • 1970-01-01
相关资源
最近更新 更多