2025年3月21日 星期五 农历 下月4日清明 English | 简体中文 | 繁體中文
查询

imagegif()函数—用法及示例

「 将图像以 GIF 格式输出或保存至文件 」


函数名称:imagegif()

适用版本:PHP 4, PHP 5, PHP 7

函数描述:imagegif() 用于将图像以 GIF 格式输出或保存至文件。

语法:bool imagegif ( resource $image [, string $filename ] )

参数:

  • $image:必需,表示图像资源,通过 imagecreate() 或 imagecreatefrom*() 函数创建。
  • $filename:可选,表示保存的文件名,如果未提供,则图像将以 GIF 格式输出到浏览器。

返回值:如果成功则返回 true,否则返回 false。

示例:

  1. 将图像以 GIF 格式输出到浏览器:
// 创建一个 200x200 的图像资源
$image = imagecreate(200, 200);

// 设置背景颜色为红色
$bgColor = imagecolorallocate($image, 255, 0, 0);

// 将图像输出为 GIF 格式
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image);
  1. 将图像以 GIF 格式保存到文件:
// 创建一个 200x200 的图像资源
$image = imagecreate(200, 200);

// 设置背景颜色为绿色
$bgColor = imagecolorallocate($image, 0, 255, 0);

// 将图像保存为 GIF 文件
imagegif($image, 'output.gif');
imagedestroy($image);

以上示例中,首先使用 imagecreate() 创建一个指定大小的图像资源。然后使用 imagecolorallocate() 设置图像的背景颜色。最后通过 imagegif() 函数将图像以 GIF 格式输出到浏览器或保存到文件中。最后使用 imagedestroy() 函数释放图像资源。

补充纠错
上一个函数: imagegrabscreen()函数
下一个函数: imagefttext()函数
热门PHP函数