3 回答

TA貢獻1906條經(jīng)驗 獲得超10個贊
快速瀏覽PHP手冊會得出以下內(nèi)容:
如果要保存從Javascript canvas.toDataURL()函數(shù)派生的數(shù)據(jù),則必須將空格轉(zhuǎn)換為加號。如果不這樣做,則解碼的數(shù)據(jù)將被破壞:
<?php
$encodedData = str_replace(' ','+',$encodedData);
$decodedData = base64_decode($encodedData);
?>

TA貢獻1934條經(jīng)驗 獲得超2個贊
您的示例中的數(shù)據(jù)URI不是有效的PNG圖片。這將永遠無法工作,并且與代碼無關(guān),與數(shù)據(jù)無關(guān)。
不適用,但可能會引起關(guān)注:
file_put_contents($_POST['logoFilename'], file_get_contents($data));
背后的想法:PHP本身可以讀取數(shù)據(jù)URI(data://)的內(nèi)容,因此您不需要自己對其進行解碼。
請注意,正式數(shù)據(jù)URI方案(參考:“ data” URL方案RFC 2397)//在冒號(“ :”)之后不包含雙斜杠(“ ”)。PHP支持帶或不帶兩個斜杠。
# RFC 2397 conform
$binary = file_get_contents($uri);
# with two slashes
$uriPhp = 'data://' . substr($uri, 5);
$binary = file_get_contents($uriPhp);

TA貢獻1803條經(jīng)驗 獲得超6個贊
所有有效的代碼:
$imgData = str_replace(' ','+',$_POST['image']);
$imgData = substr($imgData,strpos($imgData,",")+1);
$imgData = base64_decode($imgData);
// Path where the image is going to be saved
$filePath = $_SERVER['DOCUMENT_ROOT']. '/ima/temp2.png';
// Write $imgData into the image file
$file = fopen($filePath, 'w');
fwrite($file, $imgData);
fclose($file);
- 3 回答
- 0 關(guān)注
- 522 瀏覽
添加回答
舉報