(PHP 4, PHP 5)
imagegif — 輸出圖象到瀏覽器或文件。
說明
bool imagegif ( resource$image
[, string $filename
] )
imagegif() 從 image
圖像以 filename
為文件名創建一個
GIF 圖像。image
參數是 imagecreate() 或
imagecreatefrom* 函數的返回值。
圖像格式為 GIF87a。如果用了 imagecolortransparent() 使圖像為透明,則其格式為 GIF89a。
參數
image
由圖象創建函數(例如imagecreatetruecolor())返回的圖象資源。
filename
文件保存的路徑,如果未設置或為 NULL
,將會直接輸出原始圖象流。
返回值
成功時返回 TRUE
, 或者在失敗時返回 FALSE
。
范例
Example #1 使用 imagegif() 輸出一個圖像
<?php
// 創建新的圖像實例
$im = imagecreatetruecolor(100, 100);
// 設置背景為白色
imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF);
//在圖像上寫字
imagestring($im, 3, 40, 20, 'GD Library', 0xFFBA00);
// 輸出圖像到瀏覽器
header('Content-Type: image/gif');
imagegif($im);
imagedestroy($im);
?>
Example #2 使用 imagegif() 將一個 PNG 轉換成 GIF
<?php
// 載入 PNG
$png = imagecreatefrompng('./php.png');
// 以 GIF 保存圖像
imagegif($png, './php.gif');
// 釋放內存
imagedestroy($png);
// 完工
echo 'Converted PNG image to GIF with success!';
?>
注釋
Note:
不過從 GD 庫 1.6 起所有的 GIF 支持都移除了,并在版本 2.0.28 中加了回來。如果使用這些 版本之間的 GD 庫時本函數不可用。 更多信息見 » GD Project 站點。
以下代碼段通過自動檢測 GD 支持的圖像類型來寫出移植性更好的 PHP 程序。用更靈活的代碼替代了原來的 header("Content-type: image/gif"); imagegif($im);:
<?php
// 創建新的圖像實例
$im = imagecreatetruecolor(100, 100);
// 在這里對圖像進行一些操作
// 處理輸出
if(function_exists('imagegif'))
{
// 針對 GIF
header('Content-Type: image/gif');
imagegif($im);
}
elseif(function_exists('imagejpeg'))
{
// 針對 JPEG
header('Content-Type: image/jpeg');
imagejpeg($im, NULL, 100);
}
elseif(function_exists('imagepng'))
{
// 針對 PNG
header('Content-Type: image/png');
imagepng($im);
}
elseif(function_exists('imagewbmp'))
{
// 針對 WBMP
header('Content-Type: image/vnd.wap.wbmp');
imagewbmp($im);
}
else
{
imagedestroy($im);
die('No image support in this PHP server');
}
// 如果發現圖像是以上的格式之一,就從內存中釋放
if($im)
{
imagedestroy($im);
}
?>
Note:
自 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() - 以 PNG 格式將圖像輸出到瀏覽器或文件 imagewbmp() - 以 WBMP 格式將圖像輸出到瀏覽器或文件 imagejpeg() - 輸出圖象到瀏覽器或文件。 imagetypes() - 返回當前 PHP 版本所支持的圖像類型