鍍金池/ 問答/PHP/ 服務端圖片轉(zhuǎn)Base64問題

服務端圖片轉(zhuǎn)Base64問題

<?php

$str = file_get_contents('http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'); 
$str = json_decode($str,true);
$imgurl = 'http://cn.bing.com'.$str['images'][0]['url'];    //得到的圖片url


//現(xiàn)在需要把圖片轉(zhuǎn)為Base64返回給用戶
回答
編輯回答
舊城人
$str = file_get_contents('http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'); 
$str = json_decode($str,true);
$imgurl = 'http://cn.bing.com'.$str['images'][0]['url'];    //得到的圖片url

// 獲取圖片二進制流
$imageStream = file_get_contents($imgurl);
// base 64
return base64_encode($imageStream);
2017年5月1日 18:54
編輯回答
敢試
echo 'data:image/jpeg;base64,'.base64_encode(file_get_contents($imgurl));
2018年9月18日 00:14