GD 庫是 PHP 內建的圖形處理擴充,支援建立圖片、繪製幾何圖形、文字渲染、色彩操作等。本文為系列第一篇,涵蓋建立畫布、取得圖片資訊、色彩分配(含透明度)、基本繪圖函數(弧線、字元、橢圓)與輸出流程,所有範例均為可直接執行的完整腳本。
<?php
if (extension_loaded('gd')) {
$info = gd_info();
echo "GD 版本:{$info['GD Version']}\n";
echo "支援格式:" . implode(', ', array_keys(array_filter($info, function($k){return strpos($k,'Support')!==false;}, ARRAY_FILTER_USE_KEY))) . "\n";
} else {
die("請啟用 php_gd2 擴充");
}
?>
<?php
$file = './logo_i.gif';
$size = getimagesize($file);
// [0]=寬 [1]=高 [2]=型別常數 [3]=HTML屬性字串 [bits][channels][mime]
print_r($size);
// Array ( [0]=>240 [1]=>124 [2]=>1 [3]=>width="240" height="124" [bits]=>8 [channels]=>3 [mime]=>image/gif )
echo image_type_to_mime_type($size[2]); // image/gif
?>
| 函數 | 色彩深度 | 適用場景 |
|---|---|---|
imagecreatetruecolor($w, $h) | True Color (24-bit) | 現代專案預設,支援完整色彩與 Alpha 通道 |
imagecreate($w, $h) | 調色板 (8-bit, 256 色) | 舊版相容、極小圖示、記憶體受限 |
<?php
$img = imagecreatetruecolor(200, 200); // 建議優先使用
?>
<?php
$im = imagecreate(100, 100);
$red = imagecolorallocate($im, 255, 0, 0); // 整數 0-255
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); // 十六進位
// 第一次呼叫自動成為背景色
?>
<?php
$size = 300;
$image = imagecreatetruecolor($size, $size);
$back = imagecolorallocate($image, 255, 255, 255);
$border = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $size-1, $size-1, $back);
imagerectangle($image, 0, 0, $size-1, $size-1, $border);
// Alpha 0(不透明) ~ 127(全透明)
$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75);
$red = imagecolorallocatealpha($image, 255, 0, 0, 75);
$blue = imagecolorallocatealpha($image, 0, 0, 255, 75);
imagefilledellipse($image, 100, 75, 150, 150, $yellow);
imagefilledellipse($image, 120, 165, 150, 150, $red);
imagefilledellipse($image, 187, 125, 150, 150, $blue);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
| 函數 | 用途 |
|---|---|
imagecolordeallocate($im, $color) | 釋放色彩索引 |
imagecolorexact($im, $r, $g, $b) | 取得色彩索引,不存在回傳 -1 |
imagecolorset($im, $index, $r, $g, $b) | 修改調色板指定索引色彩 |
imagecolortransparent($im, $color) | 指定透明色(GIF/PNG8 必用) |
<?php
$im = imagecreate(100, 100);
$bg = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $bg); // 黑色設為透明
header('Content-type: image/png');
imagepng($im);
?>
<?php
$img = imagecreatetruecolor(200, 200);
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);
// imagearc($img, $cx, $cy, $w, $h, $start, $end, $color)
imagearc($img, 100, 100, 150, 150, 0, 360, $white); // 完整圓
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
<?php
$im = imagecreate(100, 100);
$bg = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// imagechar($im, $font, $x, $y, $char, $color) $font 1-5 內建字體
imagechar($im, 1, 0, 0, 'php', $black); // 水平 'P'
imagecharup($im, 3, 10, 10, 'Note', $black); // 垂直 'N'
header('Content-type: image/png');
imagepng($im);
?>
注意:imagechar僅繪製字串第一個字元,多行文字請用imagestring或imagettftext(TrueType 字體)。
<?php
// 1. 建立/載入圖片資源
$img = imagecreatetruecolor(200, 200);
// 2. 繪圖操作...
// 3. 發送正確 Header
header('Content-Type: image/png'); // 或 image/jpeg, image/gif
// 4. 輸出
imagepng($img); // imagejpeg($img, null, 90); imagegif($img);
// 5. 釋放記憶體
imagedestroy($img);
?>
header() 前是否有空白輸出(含 BOM),確保無 echo、錯誤訊息、關閉標籤 ?> 後的換行。imagettftext() 搭配 .ttf 字體檔,並確認字體支援中文字碼。AlphaImageLoader filter 或改用 GIF + imagecolortransparent。下一篇將深入 imagettftext 文字渲染、imagecopyresampled 縮圖與水印、imagefilter 濾鏡效果等進階應用。