从画布下载图像

时间:2013-06-19 19:30:43

标签: php javascript jquery canvas

http://www.taffatech.com/Paint.html

单击“保存”,然后单击“下载”按钮。 我试图让它下载图像并将其重命名为一个名称,在我的代码中它是'myimage'我感谢你能给我的任何帮助,谢谢!

错误:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/29/10007129/html/taffatech/saveme.php:2) in /home/content/29/10007129/html/taffatech/saveme.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at /home/content/29/10007129/html/taffatech/saveme.php:2) in /home/content/29/10007129/html/taffatech/saveme.php on line 7

php的代码:

<?php
    # we are a PNG image
    header('Content-type: image/png');

    # we are an attachment (eg download), and we have a name
    header('Content-Disposition: attachment; filename="' . $_POST['name'] .'"');

    #capture, replace any spaces w/ plusses, and decode
    $encoded = $_POST['imgdata'];
    $encoded = str_replace(' ', '+', $encoded);
    $decoded = base64_decode($encoded);

    #write decoded data
    echo $decoded;
?>

js按钮:

$("#download").click(function() {
var cs = new CanvasSaver('http://taffatech.com/saveme.php');

cs.savePNG(canvas, 'myimage');
});

保存函数js:

function CanvasSaver(url) {

    this.url = url;

    this.savePNG = function(canvas, fname) {
        if(!canvas || !url) return;
        fname = fname || 'picture';

        var data = canvas.toDataURL("image/png");
        data = data.substr(data.indexOf(',') + 1).toString();

        var dataInput = document.createElement("input") ;
        dataInput.setAttribute("name", 'imgdata') ;
        dataInput.setAttribute("value", data);
        dataInput.setAttribute("type", "hidden");

        var nameInput = document.createElement("input") ;
        nameInput.setAttribute("name", 'name') ;
        nameInput.setAttribute("value", fname + '.png');

        var myForm = document.createElement("form");
        myForm.method = 'post';
        myForm.action = url;
        myForm.appendChild(dataInput);
        myForm.appendChild(nameInput);



    document.body.appendChild(myForm) ;
    myForm.submit() ;
    document.body.removeChild(myForm) ;
};

}

谢谢!如果您需要更多信息,请询问!

1 个答案:

答案 0 :(得分:0)

问题是php的第一行,你不能在php文件的开头有一个空行。谢谢大家。