PHP 影象處理

PHP 影象處理

PHP 提供了豐富的影象處理函式,主要包括:

函式描述
gd_info() 取得當前安裝的 GD 庫的資訊
getimagesize() 獲取影象資訊
getimagesizefromstring() 獲取影象資訊
image_type_to_extension() 獲取圖片字尾
image_type_to_mime_type() 返回影象的 MIME 型別
image2wbmp() 輸出WBMP圖片
imageaffine() 返回經過仿射變換後的影象
imageaffinematrixconcat() 連線兩個矩陣
imageaffinematrixget() 獲取矩陣
imagealphablending() 設定影象的混色模式
imageantialias() 是否使用抗鋸齒(antialias)功能
imagearc() 畫橢圓弧
imagechar() 寫出橫向字元
imagecharup() 垂直地畫一個字元
imagecolorallocate() 為一幅影象分配顏色
imagecolorallocatealpha() 為一幅影象分配顏色和透明度
imagecolorat() 取得某畫素的顏色索引值
imagecolorclosest() 取得與指定的顏色最接近的顏色的索引值
imagecolorclosestalpha() 取得與指定的顏色加透明度最接近的顏色的索引
imagecolorclosesthwb() 取得與指定的顏色最接近的色度的黑白色的索引
imagesx() 、imagesy() 獲取影象寬度與高度

GD 庫

使用 PHP 影象處理函式,需要載入 GD 支援庫。請確定 php.ini 載入了 GD 庫:

Window 伺服器上:

extension = php_gd2.dll

Linux 和 Mac 系統上:

extension = php_gd2.so

使用 gd_info() 函式可以檢視當前安裝的 GD 庫的資訊:

<?php
var_dump(gd_info());
?>

輸出大致如下:

array(12) {
  ["GD Version"]=>
  string(26) "bundled (2.1.0 compatible)"
  ["FreeType Support"]=>
  bool(true)
  ["FreeType Linkage"]=>
  string(13) "with freetype"
  ["T1Lib Support"]=>
  bool(false)
  ["GIF Read Support"]=>
  bool(true)
  ["GIF Create Support"]=>
  bool(true)
  ["JPEG Support"]=>
  bool(true)
  ["PNG Support"]=>
  bool(true)
  ["WBMP Support"]=>
  bool(true)
  ["XPM Support"]=>
  bool(false)
  ["XBM Support"]=>
  bool(true)
  ["JIS-mapped Japanese Font Support"]=>
  bool(false)
}