(005)PHP 技巧 * GD 庫畫表格圖片演示

一、數據準備

下載字體;

二、示例代碼:

<?php
// 標題文本 
$title = "我是標題";
// 數據
$data = [
    ["id" => "ID", "username" => "用戶名", "score" => "得分"],
    ["id" => 1, "username" => "給你最好的我丶", "score" => 92],
    ["id" => 2, "username" => "擡首輕笑", "score" => 95],
    ["id" => 3, "username" => "嘉恆帥比等你王者歸來", "score" => 74]
];
// TODO::字體路徑
$font = dirname(__FILE__) . "/YaHei.Consolas.1.12.ttf";
$font_size = 30;
// 標題長度
$this_title_box = imagettfbbox($font_size, 0, $font, $title);
$title_x_len = $this_title_box[2] - $this_title_box[0];
$title_height = 60;

// 每行高度
$row_hight = $title_height - 10;
$id_x_len = $username_x_len = $score_x_len = 20;
foreach ($data as $key => $value) {
    # imagettfbbox 獲取文字 4 個角的座標
    $this_id_box = imagettfbbox($font_size, 0, $font, $value["id"]);
    $this_username_box = imagettfbbox($font_size, 0, $font, $value["username"]);
    $this_score_box = imagettfbbox($font_size, 0, $font, $value["score"]);

    // 每列x軸長度
    $data[$key]["this_id_x_len"] = $this_id_x_len = $this_id_box[2] - $this_id_box[0];
    $data[$key]["this_username_x_len"] = $this_username_x_len = $this_username_box[2] - $this_username_box[0];
    $data[$key]["this_score_x_len"] = $this_score_x_len = $this_score_box[2] - $this_score_box[0];

    // 最長寬度作表格寬度
    $id_x_len = $this_id_x_len > $id_x_len ? $this_id_x_len : $id_x_len;
    $username_x_len = $this_username_x_len > $username_x_len ? $this_username_x_len : $username_x_len;
    $score_x_len = $this_score_x_len > $score_x_len ? $this_score_x_len : $score_x_len;
}
// 列數
$column = 3;
// 文本左右內邊距
$x_padding = 50;
$y_padding = 10;
// 圖片寬度(每列寬度 + 每列左右內邊距)
$img_width = ($id_x_len + $username_x_len + $score_x_len) + $x_padding * $column * 2;
// 圖片高度(標題高度 + 每行高度 + 每行內邊距)
$img_height = $title_height + count($data) * ($row_hight + $y_padding);

# 開始畫圖
// 創建畫布
$img = imagecreatetruecolor($img_width, $img_height);

# 創建畫筆
// 背景顏色(藍色)
$bg_color = imagecolorallocate($img, 24, 98, 229);
// 表面顏色(淺灰)
$surface_color = imagecolorallocate($img, 235, 242, 255);
// 標題字體顏色(白色)
$title_color = imagecolorallocate($img, 255, 255, 255);
// 內容字體顏色(灰色)
$text_color = imagecolorallocate($img, 152, 151, 152);


// 畫矩形 (先填充一個大背景,小一點的矩形形成外邊框)
imagefill($img, 0, 0, $bg_color);
imagefilledrectangle($img, 2, $title_height, $img_width - 3, $img_height - 3, $surface_color);

// 畫豎線
imageline($img, $id_x_len + $x_padding * 2, $title_height, $id_x_len + $x_padding * 2, $img_height, $bg_color);
// 畫豎線
imageline($img, $id_x_len + $username_x_len + $x_padding * 4, $title_height, $id_x_len + $username_x_len + $x_padding * 4, $img_height, $bg_color);

// 寫入標題
imagettftext($img, $font_size, 0, $img_width / 2 - $title_x_len / 2, $title_height - $font_size / 2, $title_color, $font, $title);

// 寫入表格
$temp_height = $title_height;
foreach ($data as $key => $value) {
    # code...
    $temp_height += $row_hight + $y_padding;
    // 畫線
    imageline($img, 0, $temp_height, $img_width, $temp_height, $bg_color);
    // 寫入ID
    imagettftext($img, $font_size, 0, $id_x_len / 2 - $value["this_id_x_len"] / 2 + $x_padding, $temp_height - $font_size / 2, $text_color, $font, $value["id"]);
    // 寫入username
    imagettftext($img, $font_size, 0, $username_x_len / 2 - $value["this_username_x_len"] / 2 + $x_padding * 3 + $id_x_len, $temp_height - $font_size / 2, $text_color, $font, $value["username"]);
    // 寫入score
    imagettftext($img, $font_size, 0, $score_x_len / 2 - $value["this_score_x_len"] / 2 + $x_padding * 5 + $id_x_len + $username_x_len, $temp_height - $font_size / 2, $text_color, $font, $value["score"]);
}

imagepng($img, __DIR__ . "/test.jpg");
echo "<img src='./test.jpg' />";

演示效果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章