abstract:這篇文章主要介紹了PHP 將dataurl轉(zhuǎn)成圖片image方法的相關(guān)資料,這里提供了兩種方法及實(shí)現(xiàn)方式,需要的朋友可以參考下.PHP 將dataurl轉(zhuǎn)成圖片image方法使用canvas 生成的圖片,是使用dataurl的,php無法直接通過file_put_contents方法保存到本地電腦,需要做一下轉(zhuǎn)碼。 圖片dataurl 如下$imgstr = 'data:imag
這篇文章主要介紹了PHP 將dataurl轉(zhuǎn)成圖片image方法的相關(guān)資料,這里提供了兩種方法及實(shí)現(xiàn)方式,需要的朋友可以參考下.
PHP 將dataurl轉(zhuǎn)成圖片image方法
使用canvas 生成的圖片,是使用dataurl的,php無法直接通過file_put_contents方法保存到本地電腦,需要做一下轉(zhuǎn)碼。
圖片dataurl 如下
$imgstr = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
方法一:
通過正則提取出dataurl存儲(chǔ)所需的數(shù)據(jù),然后直接展示在頁面上
if (!preg_match('/data:([^;]*);base64,(.*)/', $imgstr, $matches)) { die("error"); } $content = base64_decode($matches[2]); header('Content-Type: '.$matches[1]); header('Content-Length: '.strlen($content)); echo $content; die;
方法二:
如果僅僅是想保存圖片到本地,可以用substr 和 strpos 方法
$imgdata = substr($imgstr,strpos($imgstr,",") + 1); $decodedData = base64_decode($imgdata); file_put_contents('11.png',$decodedData );
更多關(guān)于PHP 將dataurl轉(zhuǎn)成圖片image方法總結(jié)請(qǐng)關(guān)注PHP中文網(wǎng)(www.miracleart.cn)其它文章!