<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(200, 100);
$bg = imagecolorallocate($im, 255, 255, 255);
$green = imagecolorallocate($im, 0, 255, 0);
// 6點座標:[x1,y1, x2,y2, ...]
$points = [40,10, 60,10, 70,20, 60,50, 40,50, 30,20];
// 線框
imagepolygon($im, $points, 6, $green);
// 實心 (自動閉合)
imagefilledpolygon($im, $points, 6, $green);
imagepng($im);
imagedestroy($im);
?>
無需手動添加首尾相同點閉合,函數內部自動處理。
<?php
$im = imagecreatetruecolor(200, 250);
$black = imagecolorallocate($im, 0, 0, 0);
$orange = imagecolorallocate($im, 255, 192, 0);
$str = "This is a test.";
for ($size = 1; $size <= 5; $size++) {
$y = 10 + ($size-1) * 40;
imagestring($im, $size, 10, $y, $str, $orange);
}
// 垂直文本
for ($size = 1; $size <= 5; $size++) {
$x = 10 + ($size-1) * 30;
imagestringup($im, $size, $x, 180, $str, $orange);
}
// 獲取字體尺寸 (用於佈局計算)
$w = imagefontwidth(5); // 5號字寬度
$h = imagefontheight(5); // 5號字高度
?>
imagettftext<?php
$im = imagecreatetruecolor(400, 200);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 0, 0, $white);
// 需指定 .ttf 字體文件路徑
$font = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'; // 或微軟雅黑 msyh.ttc
$text = "Hello PHP GD 中文測試";
// imagettftext($im, size, angle, x, y, color, fontfile, text)
// x,y 為文本左下角基線位置
imagettftext($im, 20, 0, 20, 50, $black, $font, $text);
imagettftext($im, 20, 15, 20, 100, $black, $font, $text); // 旋轉 15°
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
**中文必須用 TTF 字體**。服務器需安裝字體文件並確保 PHP 進程可讀。
模擬數據生成日 K 線圖 (開高低收 + 成交量 + MA5/MA10/MA20)。
<?php
header('Content-Type: image/png');
// 1. 畫布與顏色
$width = 800; $height = 400;
$im = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
$grid = imagecolorallocate($im, 220, 220, 220);
$up = imagecolorallocate($im, 255, 0, 0); // 陽線 紅
$down = imagecolorallocate($im, 0, 180, 0); // 陰線 綠
$vol = imagecolorallocate($im, 100, 100, 255);
$ma5 = imagecolorallocate($im, 255, 128, 0);
$ma10 = imagecolorallocate($im, 0, 0, 255);
$ma20 = imagecolorallocate($im, 128, 0, 128);
imagefill($im, 0, 0, $white);
// 2. 模擬數據 (實際從 DB 讀取)
$data = [];
$base = 10.0;
for ($i = 0; $i < 60; $i++) {
$change = (mt_rand(-50, 50) / 100); // -0.5 ~ +0.5
$open = $base + $change;
$high = $open + mt_rand(0, 20) / 100;
$low = $open - mt_rand(0, 20) / 100;
$close = $low + mt_rand(0, ($high-$low)*100) / 100;
$vol = mt_rand(500, 5000);
$data[] = compact('open','high','low','close','vol');
$base = $close;
}
// 3. 計算座標映射參數
$marginLeft = 60; $marginRight = 20; $marginTop = 20; $marginBottom = 80; // 底部留成交量區
$chartW = $width - $marginLeft - $marginRight;
$chartH = $height - $marginTop - $marginBottom;
$volH = 60; // 成交量區高度
$klineH = $chartH - $volH; // K線區高度
$allHigh = max(array_column($data, 'high'));
$allLow = min(array_column($data, 'low'));
$priceRange = $allHigh - $allLow;
$maxVol = max(array_column($data, 'vol'));
// 價格 -> Y (K線區)
function priceToY($price) use ($marginTop, $klineH, $allHigh, $priceRange) {
return $marginTop + $klineH - (($price - $allLow) / $priceRange) * $klineH;
}
// 成交量 -> Y (成交量區)
function volToY($v) use ($height, $marginBottom, $volH, $maxVol) {
return $height - $marginBottom - ($v / $maxVol) * $volH;
}
// 索引 -> X
function idxToX($i) use ($marginLeft, $chartW, $data) {
return $marginLeft + ($i / (count($data)-1)) * $chartW;
}
// 4. 畫網格與刻度
for ($i = 0; $i <= 5; $i++) {
$y = $marginTop + ($i/5) * $klineH;
imageline($im, $marginLeft, $y, $width-$marginRight, $y, $grid);
$price = $allHigh - ($i/5) * $priceRange;
imagestring($im, 2, 5, $y-6, number_format($price, 2), $black);
}
for ($i = 0; $i < count($data); $i += 10) {
$x = idxToX($i);
imageline($im, $x, $marginTop, $x, $height-$marginBottom, $grid);
}
// 5. 畫 K 線
$barW = max(1, $chartW / count($data) * 0.6);
foreach ($data as $i => $d) {
$x = idxToX($i);
$yOpen = priceToY($d['open']);
$yClose = priceToY($d['close']);
$yHigh = priceToY($d['high']);
$yLow = priceToY($d['low']);
$color = $d['close'] >= $d['open'] ? $up : $down;
// 實體
$yTop = min($yOpen, $yClose);
$yBottom = max($yOpen, $yClose);
imagefilledrectangle($im, $x - $barW/2, $yTop, $x + $barW/2, $yBottom, $color);
// 上下影線
imageline($im, $x, $yHigh, $x, $yLow, $color);
}
// 6. 畫成交量
foreach ($data as $i => $d) {
$x = idxToX($i);
$yBase = $height - $marginBottom;
$yTop = volToY($d['vol']);
$color = $d['close'] >= $d['open'] ? $up : $down;
imagefilledrectangle($im, $x - $barW/2, $yTop, $x + $barW/2, $yBase, $color);
}
// 7. 畫 MA 線 (簡易: 收盤價移動平均)
function drawMA($period, $color) use ($im, $data, $marginLeft, $chartW, $klineH, $allHigh, $priceRange, $marginTop) {
$pts = [];
for ($i = $period-1; $i < count($data); $i++) {
$sum = 0;
for ($j = $i-$period+1; $j <= $i; $j++) $sum += $data[$j]['close'];
$ma = $sum / $period;
$x = $marginLeft + ($i / (count($data)-1)) * $chartW;
$y = $marginTop + $klineH - (($ma - $allLow) / $priceRange) * $klineH;
$pts[] = $x; $pts[] = $y;
}
if (count($pts) >= 4) imagepolygon($im, $pts, count($pts)/2, $color);
}
drawMA(5, $ma5);
drawMA(10, $ma10);
drawMA(20, $ma20);
// 8. 圖例
imagestring($im, 3, $width-120, 10, "MA5 MA10 MA20", $black);
imagepng($im);
imagedestroy($im);
?>
GD 實現 K 線圖展示了**座標變換、條件繪製、多圖層疊加**核心技巧。現代 Web 圖表首選 **ECharts/Chart.js/Highcharts (前端渲染)**,服務端 GD 適用於:郵件嵌入圖表、報表 PDF 生成、無頭瀏覽器不可用環境、SEO 靜態圖片生成。