2011-09-03 103 views
5

我很難用canvas的todataurl()方法創建數據。目前我的代碼將結果數據發送到我的php服務器,該服務器使用file_put_contents()方法創建一個文件來存儲該數據。現在,如果我從文件中剪切並粘貼得到的亂碼到一個圖像標籤src中,它可以正常工作並正常顯示,因此我認爲一切都很好。解碼畫布todataURL

但是當我嘗試在JS中使用代碼時,我一直遇到問題。我試過php的base64_decode方法,但不斷得到中斷文件。我發現這個代碼:

<?php 
    $encodedData = str_replace(' ','+',$encodedData); 
    $decocedData = base64_decode($encodedData); 

仍然有被破壞的文件。理想情況下,我想創建一個.png文件,但我會解決只是在JS中處理數據文件。任何幫助不勝感激。

+0

您只需讀取MIME類型(可能是固定/已知的)和編碼(可能沒有)來解釋數據,這是標準的Base64。如果您先前對URL進行了URL編碼,則不會有任何空間被替換。 [PHP-FileUpload](https://github.com/delight-im/PHP-FileUpload)有一個['DataUriUpload'](https://github.com/delight-im/PHP-FileUpload/blob/023f812226673ac9e0696d8a3579bb7380606dda/ src/DataUriUpload.php)組件,它自動完成所有這些。它記錄在[這裏](https://github.com/delight-im/PHP-FileUpload/tree/023f812226673ac9e0696d8a3579bb7380606dda#data-uri-uploads)。 – caw

回答

12

看來你必須擺脫由toDataURL()函數預先圖像數據的頭。 在客戶端就可以剝離這樣的標題:)這些瀏覽器

<?php 
    $decocedData = base64_decode($encodedData); 
?> 
+3

這很有幫助,這是我最終做的事情,如果有人需要在將來也這樣做:\t $ backGround = substr($ backGround,22); \t $ backGround = str_replace('','+',$ backGround); \t $ backGround = base64_decode($ backGround); \t file_put_contents(「currentBG1。png「,$ backGround); – joel

+0

需要將jpeg添加到正則表達式中:var output = data.replace(/^data:image \ /(png | jpg | jpeg); base64,/,」「); – dickyj

3

在Java腳本從canvas.toDataURL(發送結果:

.. 
var data=canvas.toDataURL(); 
var output=data.replace(/^data:image\/(png|jpg);base64,/, ""); 
// now send "output" to the server 
.. 

在服務器端使用此比默認類型的「image/png」支持這種方法。

var imageInfo = canvas.toDataURL(); 
// now send "imageInfo" to the server 

創建PNG文件直接方式:

<?php 
$imageInfo = imageInfoFromBrowser(); // Your method to get the data 
$image = fopen($imageInfo, 'r'); 
file_put_contents("fileName.png", $image); 
fclose($image); 
?> 

我在PHP 5.3這個工作/ Windows雙操作系統,其他的版本,請檢查。

用於在PHP上進行測試的圖像數據樣本。

$imageInfo = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMYAAAC0CAYAAADPcVOXAAAHx0lEQVR4Xu2dPY5dRRBGsUSI+Umdk2KQQwfMsAHERtgDrABYgCMvADJHYIeOLDZBhgjJgG6JKz2PZ+bVu7f6666qM1LLI03frqpTfd79mfF7D97jCwIQeIfAA5hAAALvEkAMdgUEbiGAGGwLCCAGewACNgKcMWycmFWMAGIUazjl2gggho0Ts4oRQIxiDadcGwHEsHFiVjECiFGs4ZRrI4AYNk7MKkYAMYo1nHJtBBDDxolZxQggRrGGU66NAGLYODGrGAHEKNZwyrURQAwbJ2YVI4AYxRpOuTYCiGHjxKxiBBCjWMMp10YAMWycmFWMAGIUazjl2gggho0Ts4oRQIxiDadcGwHEsHFiVjECiFGs4ZRrI4AYNk7MKkYAMYo1nHJtBBDDxolZxQggRrGGU66NAGLYODGrGAHEKNZwyrURQAwbJ2bdTeBZ+9HjNp5kgoQYmbo5p5YXLezDNp7OCT8mKmKM4Vpp1V9bsX0fXWcqGjEydXNOLYgxhztRFyeAGIs3yJJeyhtFS+ED5yDGQLiqpVPeKKrg3REHMSY3wCN8yiZ6gDmwRkqm1W6+UzbxwKb2ODQlU8Tw2Bq110CMBP1P2cTJfUnJlDPG5F2VIDxi0MQEBPxLQAx/pvIVUzZRTvHtgCmZcik1eVclCI8YNDEBAf8SEMOfqXzFlE2UU+RSajJy//CIAVMTAe4xTJiYdA+BlC82iMGeP0oAMY4SXOD4lE2czDUlU84Yk3dVgvCIQRMTEPAvATH8mcpXTNlEOUUe105G7h8eMWBqIsA9hgkTk3hcm3sPcMbw729Kppwx/DdKtRURI0HHUzZxcl9SMu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2frmIwGtuvXWyOr1k2rJa8omTBdkqYpzLNcQN27kikv68/3Hoj22cPmU6LfUqYt1RxOhsuxy/RYScOOd+Cdz30LD3kJ3FLpIY5y65ZjGsHPfnVnzK/8sfTYzKm5DahQQQQwibUHEIIEacXpGpkABiCGETKg4BxIjTKzIVEkAMIWxCxSGAGHF6RaZCAoghhE2oOAQQI06vyFRIADGEsAkVhwBixOkVmQoJIIYQNqHiEECMOL0iUyEBxBDCJlQcAogRp1dkKiSAGELYhIpDADHi9IpMhQQQQwibUHEIIEacXpGpkABiCGETKg4BxIjTKzIVEkAMIWxCxSGAGHF6RaZCAoghhE2oOAQQY51ePWupPG7jyTop1c0EMdbp/YuWysM2nq6TUt1MEGOd3od9y/x1EPplghh+LI+uhBhHCToejxiOMA8uhRgHAXoejhieNI+thRjH+LkejRiuOA8thhiH8PkejBi+PI+s9kc7+IM2Pmzj6zZ+ObIYxx4jgBjH+Hke/Wdb7P02PvJclLX2EUCMfdxGHPVPW/RVG9cjFmfNywggxmW8Rs3ePl+w9wMxRlG+YF3EuADW4Knbh8W/uSUOn3U+GP7N5RFDDPwk3PP2/aMb4funoN72dVMWRBncN8QYDPie5V+3n/29M/zVzuM4zEgAMYygmFaLAGLU6jfVGgkghhEU02oRQIxa/aZaIwHEMIJiWi0CiFGr31RrJIAYRlBMq0UAMWr1m2qNBP4DoE6utZfjM60AAAAASUVORK5CYII=" 
+0

如果這樣做失敗,請發佈你的PHP環境。謝謝。 –

+0

canvas.toDataUrl()...這是什麼? – realtebo

+1

'canvas.toDataUrl()'用於例如[fabric.js](http://您可以通過JavaScript獲取畫布var imageInfo = document.getElementById(「yourid」)。toDataURL();'或者通過JQuery' var imageInfo = $(「#yourid」)。toDataURL() ;' @DanielDeLeón發佈的解決方案非常有用,謝謝 – ptCoder

0

其他解決方案並沒有爲我工作。

這工作:

//image sample 
$baseFromJavascript = "data:image/png;base64,BBBFBfj42Pj4"; 

// remove the part that we don't need from the provided image and decode it 
$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $baseFromJavascript));