1. 程式人生 > 實用技巧 >php gd 生成表格 圖片

php gd 生成表格 圖片

public function create_table($params)
    {
        $data = array();//生成演示資料
        for($i = 0;$i < 10; $i++){
            $data[] = [
                'nice_name' => '測試名' . $i,
                'zhuang' => 100,
                'xian' => 50,
                'he' => 50,
                'zhuang_dui' => 500,
                'xian_dui' => 5000,
            ];
        }
 
        
$params = [ 'row' => 11,//資料的行數 'file_name' => 'xuju.png', 'title' => '資料核對表', 'table_time' => '2018-4-29 22:50:43', 'data' => $data ]; $base = [ 'border' => 10,//圖片外邊框 'file_path' => 'public/create_image/check/12/',//
圖片儲存路徑 'title_height' => 30,//報表名稱高度 'title_font_size' => 16,//報表名稱字型大小 'font_ulr' => 'resources/fonts/simhei.ttf',//字型檔案路徑 'text_size' => 12,//正文字型大小 'row_hight' => 30,//每行資料行高 'filed_id_width' => 60,//序號列的寬度 'filed_name_width' => 120,//
玩家名稱的寬度 'filed_data_width' => 100,//資料列的寬度 'table_header' => ['序號','暱稱','資料1','資料2','資料3','資料4','資料5'],//表頭文字 'column_text_offset_arr' => [45,90,55,55,55,65,65],//表頭文字左偏移量 'row_text_offset_arr' => [50,110,90,90,90,90,90],//資料列文字左偏移量 ]; $base['img_width'] = $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 5 + $base['border'] * 2;//圖片寬度 $base['img_height'] = $params['row'] * $base['row_hight'] + $base['border'] * 2 + $base['title_height'];//圖片高度 $border_top = $base['border'] + $base['title_height'];//表格頂部高度 $border_bottom = $base['img_height'] - $base['border'];//表格底部高度 $base['column_x_arr'] = [ $base['border'] + $base['filed_id_width'],//第一列邊框線x軸畫素 $base['border'] + $base['filed_id_width'] + $base['filed_name_width'],//第二列邊框線x軸畫素 $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 1,//第三列邊框線x軸畫素 $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 2,//第四列邊框線x軸畫素 $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 3,//第五列邊框線x軸畫素 $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 4,//第五列邊框線x軸畫素 $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 5,//第五列邊框線x軸畫素 ]; $img = imagecreatetruecolor($base['img_width'], $base['img_height']);//建立指定尺寸圖片 $bg_color = imagecolorallocate($img, 255, 255, 190);//設定圖片背景色 $text_coler = imagecolorallocate($img, 0, 0, 0);//設定文字顏色 $border_coler = imagecolorallocate($img, 0, 0, 0);//設定邊框顏色 $white_coler = imagecolorallocate($img, 255, 255, 255);//設定邊框顏色 imagefill($img, 0, 0, $bg_color);//填充圖片背景色 //先填充一個黑色的大塊背景 imagefilledrectangle($img, $base['border'], $base['border'] + $base['title_height'], $base['img_width'] - $base['border'], $base['img_height'] - $base['border'], $border_coler);//畫矩形 //再填充一個小兩個畫素的 背景色區域,形成一個兩個畫素的外邊框 imagefilledrectangle($img, $base['border'] + 2, $base['border'] + $base['title_height'] + 2, $base['img_width'] - $base['border'] - 2, $base['img_height'] - $base['border'] - 2, $bg_color);//畫矩形 //畫表格縱線 及 寫入表頭文字 foreach($base['column_x_arr'] as $key => $x){ imageline($img, $x, $border_top, $x, $border_bottom,$border_coler);//畫縱線 imagettftext($img, $base['text_size'], 0, $x - $base['column_text_offset_arr'][$key] + 1, $border_top + $base['row_hight'] - 8, $text_coler, $base['font_ulr'], $base['table_header'][$key]);//寫入表頭文字 } //畫表格橫線 foreach($params['data'] as $key => $item){ $border_top += $base['row_hight']; imageline($img, $base['border'], $border_top, $base['img_width'] - $base['border'], $border_top, $border_coler); imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][0] - $base['row_text_offset_arr'][0], $border_top + $base['row_hight'] - 10, $text_coler, $base['font_ulr'], $key + 1);//寫入序號 $sub = 0; foreach ($item as $value){ $sub++; imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][$sub] - $base['row_text_offset_arr'][$sub], $border_top + $base['row_hight'] - 10, $text_coler, $base['font_ulr'], $value);//寫入data資料 } } //計算標題寫入起始位置 $title_fout_box = imagettfbbox($base['title_font_size'], 0, $base['font_ulr'], $params['title']);//imagettfbbox() 返回一個含有 8 個單元的陣列表示了文字外框的四個角: $title_fout_width = $title_fout_box[2] - $title_fout_box[0];//右下角 X 位置 - 左下角 X 位置 為文字寬度 $title_fout_height = $title_fout_box[1] - $title_fout_box[7];//左下角 Y 位置- 左上角 Y 位置 為文字高度 //居中寫入標題 imagettftext($img, $base['title_font_size'], 0, ($base['img_width'] - $title_fout_width)/2, $base['title_height'], $text_coler, $base['font_ulr'], $params['title']); //寫入製表時間 imagettftext($img, $base['text_size'], 0, $base['border'], $base['title_height'], $text_coler, $base['font_ulr'], '時間:' . $params['table_time']); $save_path = $base['file_path'] . $params['file_name']; if(!is_dir($base['file_path']))//判斷儲存路徑是否存在,不存在則建立 { mkdir($base['file_path'],0777,true);//可建立多級目錄 } imagepng($img,$save_path);//輸出圖片,輸出png使用imagepng方法,輸出gif使用imagegif方法 echo '<img src="/'.$save_path.'"/>';

字型檔案路徑需要修改

public function create_table()
    {

        $data = [
            "voucherCode"=>'123123',//回單號
            "eftCardNo"=>'123123',//收款卡號
            "recAccount"=>'123123',//收款賬號
            "recAccountName"=>'萬小明',//收款姓名
            "otherAccountOpenNode"=>'農行',//收款行
            "amount"=>"100",//金額
            "amountChinese"=>"壹佰元整",//大寫金額
            "payAccount"=>"11111",//付款賬號
            "payAccountName"=>"22222",//付款姓名
            "payAccountOpenBank"=>"33333",//付款行
            "transferDate"=>"44444",//交易日期
            "transferTime"=>"55555",//交易時間
            "bizhong"=>"66666",//幣種
            "validateCode"=>"77777",//驗證碼
            "bsnCode"=>"88888",//交易程式碼
            "remark"=>"99999",//摘要
            "text"=>"00000",//附言
            "transferFlowNo"=>"121211111",//流水號
        ];
        $params = [
            'row' => 11,//資料的行數
            'file_name' => 'xuju.png',
            'title' => '電子回單',
            'table_time' => date("Y-m-d H:i:s"),
            'data' => $data
        ];
        $base = [
            'border' => 10,//圖片外邊框
            'file_path' => 'huidan/',//圖片儲存路徑
            'title_height' => 30,//報表名稱高度
            'title_font_size' => 16,//報表名稱字型大小
            'font_ulr' => ROOT_PATH.'public/assets/common/fonts/msyh.ttf',//字型檔案路徑
            'text_size' => 12,//正文字型大小
            'row_hight' => 30,//每行資料行高
            'filed_id_width' => 60,//序號列的寬度
            'filed_name_width' => 160,//玩家名稱的寬度
            'filed_data_width' => 100,//資料列的寬度
            'table_header' => ['序號', '暱稱', '資料1', '資料2', '資料3', '資料4', '資料5'],//表頭文字
            'column_text_offset_arr' => [45, 90, 55, 55, 55, 65, 65],//表頭文字左偏移量
            'row_text_offset_arr' => [50, 110, 90, 90, 90, 90, 90],//資料列文字左偏移量
            'cell_width' => 80,//最小單元格寬度
        ];

        $base['img_width'] = $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 5 + $base['border'] * 2;//圖片寬度
        $base['img_height'] = $params['row'] * $base['row_hight'] + $base['border'] * 2 + $base['title_height'];//圖片高度
        $border_top = $base['border'] + $base['title_height'];//表格頂部高度
        $border_bottom = $base['img_height'] - $base['border'];//表格底部高度
        $base['column_x_arr'] = [
            $base['border'] + $base['filed_id_width'],//第一列邊框線x軸畫素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'],//第二列邊框線x軸畫素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 1,//第三列邊框線x軸畫素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 2,//第四列邊框線x軸畫素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 3,//第五列邊框線x軸畫素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 4,//第五列邊框線x軸畫素
            $base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 5,//第五列邊框線x軸畫素
        ];

        $img = imagecreatetruecolor($base['img_width'], $base['img_height']);//建立指定尺寸圖片
        $bg_color = imagecolorallocate($img, 255, 255, 190);//設定圖片背景色
        $text_coler = imagecolorallocate($img, 0, 0, 0);//設定文字顏色
        $border_coler = imagecolorallocate($img, 0, 0, 0);//設定邊框顏色
        $white_coler = imagecolorallocate($img, 255, 255, 255);//設定邊框顏色
        imagefill($img, 0, 0, $bg_color);//填充圖片背景色
        //先填充一個黑色的大塊背景
        imagefilledrectangle($img, $base['border'], $base['border'] + $base['title_height'], $base['img_width'] - $base['border'], $base['img_height'] - $base['border'], $border_coler);//畫矩形
        //再填充一個小兩個畫素的 背景色區域,形成一個兩個畫素的外邊框
        imagefilledrectangle($img, $base['border'] + 2, $base['border'] + $base['title_height'] + 2, $base['img_width'] - $base['border'] - 2, $base['img_height'] - $base['border'] - 2, $bg_color);//畫矩形
        //畫表格縱線 及 寫入表頭文字
        //zll begin------------------
        //開始劃線
        //回單_橫線
        imageline($img, $base['border'], $border_top + $base['row_hight'], $base['img_width'] - $base['border'], $border_top + $base['row_hight'], $border_coler);
        //付款方_橫線
        imageline($img, $base['border'], $border_top + $base['row_hight']*4, ($base['img_width'] - $base['border'])/2, $border_top + $base['row_hight']*4, $border_coler);
        //收款方_橫線
        imageline($img, ($base['img_width'] - $base['border'])/2+$base['filed_name_width']/2, $border_top + $base['row_hight']*4, ($base['img_width'] - $base['border']), $border_top + $base['row_hight']*4, $border_coler);
        //付款方/收款方_小橫線_4條
        imageline($img, $base['filed_name_width']/2, $border_top + $base['row_hight']*2, ($base['img_width'] - $base['border'])/2, $border_top + $base['row_hight']*2, $border_coler);
        imageline($img, $base['filed_name_width']/2, $border_top + $base['row_hight']*3, ($base['img_width'] - $base['border'])/2, $border_top + $base['row_hight']*3, $border_coler);
        imageline($img, ($base['img_width'] - $base['border'])/2+$base['filed_name_width']/2, $border_top + $base['row_hight']*2, ($base['img_width'] - $base['border']), $border_top + $base['row_hight']*2, $border_coler);
        imageline($img, ($base['img_width'] - $base['border'])/2+$base['filed_name_width']/2, $border_top + $base['row_hight']*3, ($base['img_width'] - $base['border']), $border_top + $base['row_hight']*3, $border_coler);
        //金額_橫線
        imageline($img, $base['border'], $border_top + $base['row_hight']*5, $base['img_width'] - $base['border'], $border_top + $base['row_hight']*5, $border_coler);
        //幣種_橫線
        imageline($img, $base['border'], $border_top + $base['row_hight']*6, $base['img_width'] - $base['border'], $border_top + $base['row_hight']*6, $border_coler);
        //摘要_橫線
        imageline($img, $base['border'], $border_top + $base['row_hight']*7, $base['img_width'] - $base['border'], $border_top + $base['row_hight']*7, $border_coler);
        //時間_橫線
        imageline($img, $base['border'], $border_top + $base['row_hight']*8, $base['img_width'] - $base['border'], $border_top + $base['row_hight']*8, $border_coler);
        //中間豎線
        imageline($img, ($base['img_width'] - $base['border'])/2, $border_top+$base['row_hight'], ($base['img_width'] - $base['border'])/2, $base['img_height']-$base['border']-$base['row_hight']*3, $border_coler);
        //付款方_小豎線
        imageline($img, $base['filed_name_width']/2, $border_top+$base['row_hight'], $base['filed_name_width']/2, $border_top+$base['row_hight']*4, $border_coler);
        //付款方_大豎線
        imageline($img, $base['filed_name_width'], $border_top+$base['row_hight'], $base['filed_name_width'], $border_top+$base['row_hight']*11, $border_coler);
        //收款方_小豎線
        imageline($img, ($base['img_width'] - $base['border'])/2+$base['filed_name_width']/2, $border_top+$base['row_hight'], ($base['img_width'] - $base['border'])/2+$base['filed_name_width']/2, $border_top+$base['row_hight']*5, $border_coler);
        //收款方_大豎線
        imageline($img, ($base['img_width'] - $base['border'])/2+$base['filed_name_width'], $border_top+$base['row_hight'], ($base['img_width'] - $base['border'])/2+$base['filed_name_width'], $border_top+$base['row_hight']*8, $border_coler);
        //開始寫字------
        //回單編號
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][0] - $base['row_text_offset_arr'][0], $border_top + $base['row_hight'] - 10, $text_coler, $base['font_ulr'], "回單編號:".$data['voucherCode']);
        //付款方_文字
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][0] - $base['row_text_offset_arr'][0], $border_top + $base['row_hight']*3 - 10, $text_coler, $base['font_ulr'], "付款方");
        //付款方_賬號_文字
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width'], $border_top + $base['row_hight']*2 - 10, $text_coler, $base['font_ulr'], "賬號");
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width']*2+10, $border_top + $base['row_hight']*2 - 10, $text_coler, $base['font_ulr'], $data['payAccount']);
        //付款方_戶名_文字
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width'], $border_top + $base['row_hight']*3 - 10, $text_coler, $base['font_ulr'], "戶名");
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width']*2+10, $border_top + $base['row_hight']*3 - 10, $text_coler, $base['font_ulr'], $data['payAccountName']);
        //付款方_開戶行_文字
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width'], $border_top + $base['row_hight']*4 - 10, $text_coler, $base['font_ulr'], "開戶行");
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width']*2+10, $border_top + $base['row_hight']*4 - 10, $text_coler, $base['font_ulr'], $data['payAccountOpenBank']);
        //收款方_文字
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][3] - $base['row_text_offset_arr'][0], $border_top + $base['row_hight']*3 - 10, $text_coler, $base['font_ulr'], "收款方");
        //收款方_賬號_文字
        imagettftext($img, $base['text_size'], 0, ($base['img_width'] - $base['border'])/2+$base['cell_width']+10, $border_top + $base['row_hight']*2 - 10, $text_coler, $base['font_ulr'], "賬號");
        imagettftext($img, $base['text_size'], 0, $base['img_width']/2+$base['cell_width']*2+10, $border_top + $base['row_hight']*2 - 10, $text_coler, $base['font_ulr'], $data['recAccount']);
        //收款方_戶名_文字
        imagettftext($img, $base['text_size'], 0, ($base['img_width'] - $base['border'])/2+$base['cell_width']+10, $border_top + $base['row_hight']*3 - 10, $text_coler, $base['font_ulr'], "戶名");
        imagettftext($img, $base['text_size'], 0, $base['img_width']/2+$base['cell_width']*2+10, $border_top + $base['row_hight']*3 - 10, $text_coler, $base['font_ulr'], $data['recAccountName']);
        //收款方_開戶行_文字
        imagettftext($img, $base['text_size'], 0, ($base['img_width'] - $base['border'])/2+$base['cell_width']+10, $border_top + $base['row_hight']*4 - 10, $text_coler, $base['font_ulr'], "開戶行");
        imagettftext($img, $base['text_size'], 0, $base['img_width']/2+$base['cell_width']*2+10, $border_top + $base['row_hight']*4 - 10, $text_coler, $base['font_ulr'], $data['otherAccountOpenNode']);
        //收款方卡號_文字
        imagettftext($img, $base['text_size'], 0, ($base['img_width'] - $base['border'])/2+$base['cell_width']+10, $border_top + $base['row_hight']*5 - 10, $text_coler, $base['font_ulr'], "卡號");
        imagettftext($img, $base['text_size'], 0, $base['img_width']/2+$base['cell_width']*2+10, $border_top + $base['row_hight']*5 - 10, $text_coler, $base['font_ulr'], $data['eftCardNo']);
        //金額_小寫_文字
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][0] - $base['row_text_offset_arr'][0]+10, $border_top + $base['row_hight']*5 - 10, $text_coler, $base['font_ulr'], "金額(小寫)");
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width']*2+10, $border_top + $base['row_hight']*5 - 10, $text_coler, $base['font_ulr'], $data['amount']);
        //金額_大寫_文字
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][0] - $base['row_text_offset_arr'][0]+10, $border_top + $base['row_hight']*6 - 10, $text_coler, $base['font_ulr'], "金額(大寫)");
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width']*2+10, $border_top + $base['row_hight']*6 - 10, $text_coler, $base['font_ulr'], $data['amountChinese']);
        //交易渠道_文字
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][3] - $base['row_text_offset_arr'][0]+10, $border_top + $base['row_hight']*6 - 10, $text_coler, $base['font_ulr'], "交易渠道");
        imagettftext($img, $base['text_size'], 0, $base['img_width']/2+$base['cell_width']*2+10, $border_top + $base['row_hight']*6 - 10, $text_coler, $base['font_ulr'], $data['bsnCode']);
        //摘要_文字
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][0] - $base['row_text_offset_arr'][0]+10, $border_top + $base['row_hight']*7 - 10, $text_coler, $base['font_ulr'], "摘要");
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width']*2+10, $border_top + $base['row_hight']*7 - 10, $text_coler, $base['font_ulr'], $data['remark']);
        //驗證碼_文字
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][3] - $base['row_text_offset_arr'][0]+10, $border_top + $base['row_hight']*7 - 10, $text_coler, $base['font_ulr'], "驗證碼");
        imagettftext($img, $base['text_size'], 0, $base['img_width']/2+$base['cell_width']*2+10, $border_top + $base['row_hight']*7 - 10, $text_coler, $base['font_ulr'], $data['validateCode']);
        //交易時間_文字
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][0] - $base['row_text_offset_arr'][0]+10, $border_top + $base['row_hight']*8 - 10, $text_coler, $base['font_ulr'], "交易時間");
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width']*2+10, $border_top + $base['row_hight']*8 - 10, $text_coler, $base['font_ulr'], $data['transferDate'].$data['transferTime']);
        //幣種_文字
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][3] - $base['row_text_offset_arr'][0]+10, $border_top + $base['row_hight']*8 - 10, $text_coler, $base['font_ulr'], "幣種");
        imagettftext($img, $base['text_size'], 0, $base['img_width']/2+$base['cell_width']*2+10, $border_top + $base['row_hight']*8 - 10, $text_coler, $base['font_ulr'], $data['bizhong']);
        //附言_文字
        imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][0] - $base['row_text_offset_arr'][0]+10, $border_top + $base['row_hight']*10 - 10, $text_coler, $base['font_ulr'], "附言");
        imagettftext($img, $base['text_size'], 0, $base['border']+$base['cell_width']*2+10, $border_top + $base['row_hight']*9 - 10, $text_coler, $base['font_ulr'], $data['transferDate'].$data['text']);
        //zll end--------------------------
//        foreach ($base['column_x_arr'] as $key => $x) {
//            imageline($img, $x, $border_top, $x, $border_bottom, $border_coler);//畫縱線
//            imagettftext($img, $base['text_size'], 0, $x - $base['column_text_offset_arr'][$key] + 1, $border_top + $base['row_hight'] - 8, $text_coler, $base['font_ulr'], $base['table_header'][$key]);//寫入表頭文字
//        }
        //畫表格橫線
//        foreach ($params['data'] as $key => $item) {
//            $border_top += $base['row_hight'];
//            imageline($img, $base['border'], $border_top, $base['img_width'] - $base['border'], $border_top, $border_coler);
//
//            imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][0] - $base['row_text_offset_arr'][0], $border_top + $base['row_hight'] - 10, $text_coler, $base['font_ulr'], $key + 1);//寫入序號
//            $sub = 0;
//            foreach ($item as $value) {
//                $sub++;
//                imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][$sub] - $base['row_text_offset_arr'][$sub], $border_top + $base['row_hight'] - 10, $text_coler, $base['font_ulr'], $value);//寫入data資料
//            }
//        }

        //計算標題寫入起始位置
        $title_fout_box = imagettfbbox($base['title_font_size'], 0, $base['font_ulr'], $params['title']);//imagettfbbox() 返回一個含有 8 個單元的陣列表示了文字外框的四個角:
        $title_fout_width = $title_fout_box[2] - $title_fout_box[0];//右下角 X 位置 - 左下角 X 位置 為文字寬度
        $title_fout_height = $title_fout_box[1] - $title_fout_box[7];//左下角 Y 位置- 左上角 Y 位置 為文字高度
        //居中寫入標題
        imagettftext($img, $base['title_font_size'], 0, ($base['img_width'] - $title_fout_width) / 2, $base['title_height'], $text_coler, $base['font_ulr'], $params['title']);
        //寫入製表時間
        imagettftext($img, $base['text_size'], 0, $base['border'], $base['title_height'], $text_coler, $base['font_ulr'], '時間:' . $params['table_time']);

//        $save_path = $base['file_path'] . $params['file_name'];
//
//        if (!is_dir($base['file_path']))//判斷儲存路徑是否存在,不存在則建立
//        {
//            mkdir($base['file_path'], 0777, true);//可建立多級目錄
//        }
//
//        imagepng($img, $save_path);//輸出圖片,輸出png使用imagepng方法,輸出gif使用imagegif方法
//
//        echo '<img src="/' . $save_path . '"/>';
//        header("Content-type:image/png");
        header('Content-Type: image/png');
        imagepng($img);
        die;//tp5框架會自動增加頭資訊,需要die;
    }