[TOC] #### 1. GD庫做的圖片 --- **用 GD庫做的圖片,并輸出到瀏覽器上** ```php <?php $file = imagecreate(100,50); $color = imagecolorallocate($file,255,255,255); $c = imagecolorallocate($file,0,100,255); imagefill($file, 0, 0, $c); imagechar($file, 10, 20, 20,'pk',$color); imagechar($file, 10, 40, 20,'h', $color); imagechar($file, 10, 60, 20,'p', $color); header('Content-Type:image/png'); imagepng($file); ``` **頁面效果** ![](https://img.itqaq.com/art/content/208efc8bb534d39dbd52565ef5004571.png) #### 2. 請求圖片接口,使用 base64_encode() 函數(shù)處理 ---- ```php <?php $url = 'http://127.0.0.1/1.php'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); $data='image/png;base64,'.base64_encode($output); echo '<img src="data:'.$data.'">'; curl_close($ch); ``` **頁面效果** ![](https://img.itqaq.com/art/content/f52d6d8b5a9e79612249717c26857d0c.png) #### 3. 錯誤示例 ---- 接口返回的是二進制流的數(shù)據(jù),所以直接打印會亂碼 ```php <?php $url = 'http://127.0.0.1/1.php'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); // 直接打印會亂碼 var_dump($output); curl_close($ch); ``` **亂碼現(xiàn)象** ![](https://img.itqaq.com/art/content/d3a381d581f1c04fc7fc85e9f22febc1.png)