php - 调整大小并保存 html2canvas 生成的图像

标签 php javascript jquery image-resizing html2canvas

这是我正在使用的脚本

<script src="http://code.jquery.com/jquery.min.js"></script> 
<script>window.jQuery||document.write('<script src="js/jquery-1.7.1.min.js"><\/script>');</script>
<script type="text/javascript" src="http://html2canvas.hertzen.com/js/html2canvas.js?rev032"></script> 
<script type="text/javascript">
    var date=new Date();
    var message,timeoutTimer,timer;
    var proxyUrl="http://html2canvas.appspot.com";

    function addRow(a,c,d){var b=$("<tr />").appendTo($(a));b.append($("<td />").css("font-weight","bold").text(c)).append($("<td />").text(d))}

    function throwMessage(b,a){
        window.clearTimeout(timeoutTimer);
        timeoutTimer=window.setTimeout(function(){
            message.fadeOut(function(){message.remove()})
        },
        a||2000);

        $(message).remove();
        message=$("<div />").html(b).css({
            margin:0,
            padding:10,
            background:"#000",
            opacity:0.7,
            position:"fixed",
            top:10,right:10,
            fontFamily:"Tahoma",
            color:"#fff",
            fontSize:12,
            borderRadius:12,
            width:"auto",
            height:"auto",
            textAlign:"center",
            textDecoration:"none"
        }).hide().fadeIn().appendTo("body");
    }

    $(function(){$("#recommended a").click(function(c){
            c.preventDefault();
            $("#url").val(this.href);
            $("button").click()
        });

        var a,b;$('input[type="button"]').click(function(){$(a.contentWindow).unbind("load");$(a).contents().find("body").html2canvas({canvasHeight:b.body.scrollHeight,canvasWidth:b.body.scrollWidth,logging:true})});

        $("#getscreenshot").click(function(d){
            d.preventDefault();
            $(this).prop("disabled",true);
            var c=$("#url").val();
            $("#content").append($("<img />").attr("src","http://html2canvas.hertzen.com/loading.gif").css("margin-top",40));
            var f=document.createElement("a");
            f.href=c;
            $.ajax({
                data:{xhr2:false,url:f.href},url:proxyUrl,dataType:"jsonp",success:function(e){
                    a=document.createElement("iframe");
                    $(a).css({visibility:"hidden"}).width($(window).width()).height($(window).height());
                    $("#content").append(a);
                    b=a.contentWindow.document;
                    b.open();$(a.contentWindow).load(function(){
                        var g=$(a).contents().find("body"),h={onrendered:function(j){
                            $("#content").empty().append(j);
                            $("#getscreenshot").prop("disabled",false);
                            $("base").attr("href","")
                        }, 
                        allowTaint:true,
                        taintTest:false,
                        flashcanvas:"src/flashcanvas.min.js"},
                        i=html2canvas(g,h)
                    });
                    $("base").attr("href",f.protocol+"//"+f.hostname+"/"+f.pathname);
                    e=e.replace("<head>","<head><base href='"+f.protocol+"//"+f.hostname+"/"+f.pathname+"'  />");

                    if($("#disablejs").prop("checked")){
                        e=e.replace(/\<script/gi,"<!--<script");
                        e=e.replace(/\<\/script\>/gi,"<\/script>-->")
                    }
                    b.write(e);b.close()
                }
            });
        })
    });
</script> 

HTML

<h1>Html2Canvas javaScript screenshot creator</h1>
<form class="well form-search"> <label for=url>Website URL:</label> 
    <input type=url id=url value="http://www.google.co.in" class="input-medium search-query"/><button class=btn id=getscreenshot>Get screenshot!</button> 
</form> 
<label for="disablejs">Disable JavaScript (recommended, doesn't work well with the proxy)</label>
<input id="disablejs" type="checkbox" checked="">
<br>
<div id=content></div> <!-- Image will be displayed here-->

现在它正在生成 html 正文内容的屏幕截图

我需要调整图像大小并(存储到/上传到)本地服务器

最佳答案

您需要做的是通过使用 ajax 或提交表单将图像源发送到服务器。要获取源代码,请使用类似的东西:

var src = $("#your-image").attr('src');
$.post(url, {'source':src}, function(data){}); 

在 php 中:

$img = $_POST['source'];
file_put_contents('image_file', base64_decode($img));

尚未测试代码,但应该让您走上正确的轨道。

关于php - 调整大小并保存 html2canvas 生成的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11577908/

相关文章:

javascript - 使用 XMLHttpRequest (vanillaJS) 将数据发送到 PHP 并使用 $_POST 在 php 中读取数据?不明确的

php - 上传到S3,如何判断是否传输成功?

php - 如何获取当前日期时间和 HH :MM:SS 中的时间戳值之间的时间差

javascript - 如何在 React 中显示进度值

JavaScript Regex - 如何排除匹配中返回的尾随特殊字符?

php - 如何使用php在postgresql表中插入timestampz值

javascript - If 子句无法正常工作

jquery - 子div和父div有相同的类名,如何选择父div?

javascript - 表单内提交函数

javascript - 将最终给定数量的元素移动到数组的前面