第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從base 64數(shù)據(jù)字符串保存png映像服務(wù)器端

如何從base 64數(shù)據(jù)字符串保存png映像服務(wù)器端

慕碼人8056858 2019-06-25 13:42:55
如何從base 64數(shù)據(jù)字符串保存png映像服務(wù)器端我正在使用Nihilogic的“Canvas2Image”JavaScript工具將畫(huà)布繪圖轉(zhuǎn)換為PNG圖像。我現(xiàn)在需要的是使用PHP將這個(gè)工具生成的base 64字符串轉(zhuǎn)換為服務(wù)器上的實(shí)際PNG文件。簡(jiǎn)而言之,我目前正在做的是使用Canvas2Image在客戶端生成一個(gè)文件,然后檢索Base 64編碼的數(shù)據(jù)并使用Ajax將其發(fā)送到服務(wù)器:// Generate the image filevar image = Canvas2Image.saveAsPNG(canvas, true);    image.id = "canvasimage";canvas.parentNode.replaceChild(image, canvas);var url = 'hidden.php',data = $('#canvasimage').attr('src');$.ajax({      type: "POST",      url: url,     dataType: 'text',     data: {         base64data : data    }});此時(shí),“hidden.php”接收的數(shù)據(jù)塊如下所示資料來(lái)源:PNG;BASE 64,iVBO盧旺達(dá)0KGgoAAAANSUHEUAABE.從現(xiàn)在開(kāi)始,我很困惑。據(jù)我所讀,我認(rèn)為我應(yīng)該使用PHP鏡像字符串函數(shù),但我不知道如何從Base 64編碼的字符串創(chuàng)建實(shí)際的PNG映像并將其存儲(chǔ)在我的服務(wù)器上。請(qǐng)幫忙!
查看完整描述

3 回答

?
尚方寶劍之說(shuō)

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊

您需要從該字符串中提取base 64映像數(shù)據(jù),對(duì)其進(jìn)行解碼,然后可以將其保存到磁盤(pán)中,您不需要GD,因?yàn)樗呀?jīng)是一個(gè)PNG。

$data = 'data:image/png;base64,AAAFBfj42Pj4';list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);$data = base64_decode($data);file_put_contents('/tmp/image.png', $data);

作為一個(gè)整體:

$data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data));

提取、解碼和檢查錯(cuò)誤的一種有效方法是:

if (preg_match('/^data:image\/(\w+);base64,/', $data, $type)) {
    $data = substr($data, strpos($data, ',') + 1);
    $type = strtolower($type[1]); // jpg, png, gif

    if (!in_array($type, [ 'jpg', 'jpeg', 'gif', 'png' ])) {
        throw new \Exception('invalid image type');
    }

    $data = base64_decode($data);

    if ($data === false) {
        throw new \Exception('base64_decode failed');
    }} else {
    throw new \Exception('did not match data URI with image data');}file_put_contents("img.{$type}", $data);


查看完整回答
反對(duì) 回復(fù) 2019-06-25
?
RISEBY

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊

試試這個(gè):

file_put_contents('img.png', base64_decode($base64string));

文件放置內(nèi)容文檔


查看完整回答
反對(duì) 回復(fù) 2019-06-25
?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

我不得不用加號(hào)替換空格str_replace(' ', '+', $img);才能讓它發(fā)揮作用。

這是完整的代碼

$img = $_POST['img']; // Your data 'data:image/png;base64,AAAFBfj42Pj4';$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);$data = base64_decode($img);file_put_contents('/tmp/image.png', $data);

希望能幫上忙。


查看完整回答
反對(duì) 回復(fù) 2019-06-25
  • 3 回答
  • 0 關(guān)注
  • 567 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)