GD (Graphics Draw) 是 PHP 內建的圖形處理擴展,支援 JPEG、PNG、GIF、WBMP、XPM 等格式的建立、修改、輸出,廣泛應用於驗證碼生成、縮圖、水印、圖表繪製等場景。預設未啟用,需手動配置。
php.ini (通常在 PHP 安裝目錄或 C:\Windows\)extension=php_gd2.dll;,變為:extension=php_gd2.dllextension_dir 指向正確的 ext 目錄 (如 D:\PHP\ext)phpinfo() 的 Configure Command 未顯示 GD原因: Windows 版 PHP 為預編譯二進制,GD 已靜態編譯或共享庫形式內建,Configure Command 僅反映編譯時參數,不影響實際功能。只要 phpinfo() 顯示 GD Support: enabled 即為成功。
# Debian/Ubuntu
sudo apt-get install php-gd # PHP 7.x/8.x
sudo apt-get install php5-gd # PHP 5.x (舊版)
# CentOS/RHEL
sudo yum install php-gd # PHP 5.4-7.x
sudo dnf install php-gd # PHP 7.2+/8.x
安裝完成自動生成 /etc/php.d/gd.ini 或修改 php.ini,重啟 php-fpm/httpd 即可。
# 安裝依賴庫
sudo yum install libjpeg-devel libpng-devel freetype-devel libwebp-devel
# PHP 編譯參數新增
./configure \
--with-gd \
--with-jpeg-dir=/usr/lib \
--with-png-dir=/usr/lib \
--with-freetype-dir=/usr/include/freetype2 \
--with-webp-dir=/usr/lib \
--enable-gd-native-ttf \
... (其他參數)
make && make install
--with-gd:啟用 GD--with-freetype-dir:支援 TrueType 字體 (中文驗證碼必備)--enable-gd-native-ttf:使用 FreeType 而非內建字體php -m | grep gd
# 輸出 gd 表示已載入
<?php
if (extension_loaded('gd')) {
echo 'GD 擴展已載入
';
$info = gd_info();
foreach ($info as $key => $val) {
echo "$key: " . ($val ? 'Yes' : 'No') . "
";
}
} else {
echo 'GD 擴展未安裝或未啟用';
}
?>
GD 擴展已載入 GD Version: bundled (2.1.0 compatible) FreeType Support: Yes FreeType Linkage: with freetype T1Lib Support: No GIF Read Support: Yes GIF Create Support: Yes JPEG Support: Yes PNG Support: Yes WBMP Support: Yes XPM Support: Yes XBM Support: Yes WebP Support: Yes JIS-mapped Japanese Font Support: No
| 特性 | 用途 | 所需依賴 |
|---|---|---|
| FreeType Support | TTF 字體渲染 (中文驗證碼) | freetype-devel |
| JPEG Support | JPEG 讀寫 | libjpeg-devel |
| PNG Support | PNG 讀寫、透明度 | libpng-devel |
| WebP Support | WebP 格式 (現代 Web 圖片) | libwebp-devel |
| GIF Create Support | 動態圖生成 | 內建 |
| 現象 | 可能原因 | 解決方案 |
|---|---|---|
| 啟用後 Apache/IIS 無法啟動 | php_gd2.dll 版本與 PHP 核心不匹配 (VC6/VC9、TS/NTS) | 重新下載與 PHP 版本完全一致的擴展包,或使用官方完整安裝包 |
Linux 編譯報錯 freetype.h not found | 缺少 freetype-devel 或路徑錯誤 | 安裝開發包,指定正確 --with-freetype-dir |
| 生成圖片中文亂碼/方塊 | 未啟用 FreeType、字體檔路徑錯誤、字體不支援中文 | 確認 FreeType Support: Yes,使用完整路徑載入 .ttf 字體 (如微軟雅黑) |
imagecreatefromjpeg() 失敗 | JPEG 支援未編譯、圖片損壞、CMYK 色彩模式不支援 | 確認 JPEG Support: Yes,轉換圖片為 RGB 模式 |
<?php
session_start();
header('Content-Type: image/png');
$width = 120; $height = 40;
$img = imagecreatetruecolor($width, $height);
// 背景噪點
$bg = imagecolorallocate($img, 240, 240, 240);
imagefill($img, 0, 0, $bg);
for ($i = 0; $i < 50; $i++) {
$color = imagecolorallocate($img, rand(150,200), rand(150,200), rand(150,200));
imagesetpixel($img, rand(0,$width), rand(0,$height), $color);
}
// 干擾線
for ($i = 0; $i < 5; $i++) {
$color = imagecolorallocate($img, rand(100,180), rand(100,180), rand(100,180));
imageline($img, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height), $color);
}
// 繪製文字 (需 FreeType)
$font = __DIR__ . '/fonts/msyh.ttc'; // 微軟雅黑
$text = '';
for ($i = 0; $i < 4; $i++) {
$char = substr('ABCDEFGHJKLMNPQRSTUVWXYZ23456789', rand(0,31), 1);
$text .= $char;
$color = imagecolorallocate($img, rand(0,100), rand(0,100), rand(0,100));
$size = 20; $angle = rand(-15,15);
$x = 10 + $i * 25; $y = 30;
imagettftext($img, $size, $angle, $x, $y, $color, $font, $char);
}
$_SESSION['captcha'] = strtoupper($text);
imagepng($img);
imagedestroy($img);
?>
啟用 GD 庫核心在於:Windows 修改 php.ini 載入 dll、Linux 安裝 php-gd 套件或編譯參數加入 --with-gd、確認 FreeType/JPEG/PNG 依賴庫完整。驗證請以 gd_info() 為準,而非 Configure Command。掌握 GD 為 PHP 圖片處理、驗證碼、圖表等功能奠定基礎。