imagegif

(PHP 3, PHP 4, PHP 5)

imagegif -- 以 GIF 格式將圖像輸出到瀏覽器或檔案

說明

bool imagegif ( resource image [, string filename] )

imagegif()image 圖像以 filename 為檔案名建立一個 GIF 圖像。image 參數是 imagecreatetruecolor() 函數的返回值。

圖像格式為 GIF87a。若果用了 imagecolortransparent() 使圖像為透明,則其格式為 GIF89a

filename 參數為可選,若果省略,則原始圖像流將被直接輸出。通過 header() 傳送 Content-type: image/gif 可以使 PHP 腳本直接輸出 GIF 圖像。

注: 不過從 GD 庫 1.6 起所有的 GIF 支援都移除了,若果使用這些 GD 庫時本函數不可用。希望在 2004 年中期能夠發佈支援 GIF 的 GD 庫。更多訊息見 GD Project 站台。

以下代碼段通過自動檢驗 GD 支援的圖像類型來寫出移植性更好的 PHP 程式。用更靈活的代碼替代了原來的 header("Content-type: image/gif"); imagegif($im);

<?php
if (function_exists("imagegif")) {
    
header("Content-type: image/gif");
    
imagegif($im);
} elseif (
function_exists("imagejpeg")) {
    
header("Content-type: image/jpeg");
    
imagejpeg($im""0.5);
} elseif (
function_exists("imagepng")) {
    
header("Content-type: image/png");
    
imagepng($im);
} elseif (
function_exists("imagewbmp")) {
    
header("Content-type: image/vnd.wap.wbmp");
    
imagewbmp($im);
} else {
    die(
"No image support in this PHP server");
}

注: 自 PHP 3.0.18 和 4.0.2 起可以用 imagetypes() 函數代替 function_exists() 來檢查是否支援某種圖像格式:

<?php
if (imagetypes() & IMG_GIF) {
    
header ("Content-type: image/gif");
    
imagegif ($im);
} elseif (
imagetypes() & IMG_JPG) {
    
/* ... etc. */
}

參見 imagepng()imagewbmp()imagejpeg()imagetypes()