萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

Windows Server PHP GD 擴展啟用失敗排查指南

現象

phpinfo() 中 Configure Command 未顯示 --with-gd 或 --enable-gd,gd 區塊缺失,function_exists('gd_info') 回傳 false。

常見原因與解決

原因檢查方式解決步驟
php.ini 未啟用擴展php -m | findstr gd 無輸出編輯 php.ini:
extension=gd (PHP 7.2+)
extension=php_gd2.dll (舊版)
DLL 檔案缺失/版本不符php -v 顯示 VC15 x64,但 dll 為 VC11 x86下載與 PHP 版本、VC 編譯器、執行緒安全性 (TS/NTS) 完全一致的 DLL
依賴 DLL 缺失 (libpng, freetype, jpeg, libwebp, libxpm)事件檢視器 / Dependency Walker 顯示缺失將依賴 DLL 放入 PHP 目錄或系統 PATH
extension_dir 路徑錯誤php -i | findstr extension_dir設定正確絕對路徑,重啟服務
CGI/FastCGI 快取舊配置修改 ini 後未重啟重啟 php-fpm / IIS / Apache
權限問題PHP 進程無法讀取 dll/依賴授予 IIS_IUSRS / 執行身份 讀取執行權限

快速驗證腳本

<?php
// test_gd.php
if (!extension_loaded('gd')) {
    die("GD 未載入\n");
}
$info = gd_info();
print_r($info);

// 實際繪圖測試
$im = imagecreatetruecolor(200, 100);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 199, 99, $white);
imagestring($im, 5, 50, 40, 'GD OK', $black);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

Windows 專屬注意事項

  1. Thread Safety:Apache 模組需 Thread Safe (TS),PHP-FPM/CLI 用 Non-Thread Safe (NTS)。
  2. Visual C++ Redistributable:PHP 8.x 需 VC15 (VS2017) 或 VC16 (VS2019) 運行庫。
  3. PATH 環境變數:PHP 目錄需在系統 PATH 中,以便載入依賴 DLL。
  4. 多 PHP 版本共存:各版本獨立目錄、獨立 php.ini、獨立 extension_dir,切勿混用 DLL。

替代方案

  • Imagick (ImageMagick):功能更強、格式支援更多,同樣需 DLL。
  • GD 純 PHP 實作:php-gd 純 PHP 版 (效能極差,僅限極簡場景)。
  • Docker 容器化:php:8.2-apache 官方映像已內建 GD、完整依賴,避免 Windows 環境地獄。
copyright © 萬盛學電腦網 all rights reserved