0
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) ;
};
}
謝謝!如果您需要更多信息,請詢問!
你能否確認你在'saveme.php'開頭沒有任何空格? – Graham
事實上,基於hexdump,它看起來像在你的初始'<?php'之前有一個換行符。 – EPB
感謝大家換行是問題,不知道線路造成的問題! –