(PHP 4, PHP 5)
imagegif — 输出图象到浏览器或文件
$image
[, string $filename
] )
imagegif() 从 image
图像以 filename
为文件名创建一个
GIF 图像。image
参数是 imagecreate() 或
imagecreatefrom* 函数的返回值。
图像格式为 GIF87a。如果用了 imagecolortransparent() 使图像为透明,则其格式为 GIF89a。
filename
参数为可选,如果省略,则原始图像流将被直接输出。通过
header() 发送 Content-type: image/gif 可以使 PHP
脚本直接输出 GIF 图像。
Note:
不过从 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");
}
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(), imagewbmp(), imagejpeg() 和 imagetypes()。
成功时返回 TRUE
, 或者在失败时返回 FALSE
.
Example #1 Outputting an image using imagegif()
<?php
// Create a new image instance
$im = imagecreatetruecolor(100, 100);
// Make the background white
imagefilledrectangle($im, 0, 0, 99, 99, 0xFFFFFF);
// Draw a text string on the image
imagestring($im, 3, 40, 20, 'GD Library', 0xFFBA00);
// Output the image to browser
header('Content-Type: image/gif');
imagegif($im);
imagedestroy($im);
?>
Example #2 Converting a PNG image to GIF using imagegif()
<?php
// Load the PNG
$png = imagecreatefrompng('./php.png');
// Save the image as a GIF
imagegif($png, './php.gif');
// Free from memory
imagedestroy($png);
// We're done
echo 'Converted PNG image to GIF with success!';
?>
Note:
GIF support was removed from the GD library in Version 1.6, and added back in Version 2.0.28. This function is not available between these versions. For more information, see the » GD Project site.
The following code snippet allows you to write more portable PHP applications by auto-detecting the type of GD support which is available. Replace the sequence header ("Content-Type: image/gif"); imagegif ($im); by the more flexible sequence:
<?php
// Create a new image instance
$im = imagecreatetruecolor(100, 100);
// Do some image operations here
// Handle output
if(function_exists('imagegif'))
{
// For GIF
header('Content-Type: image/gif');
imagegif($im);
}
elseif(function_exists('imagejpeg'))
{
// For JPEG
header('Content-Type: image/jpeg');
imagejpeg($im, NULL, 100);
}
elseif(function_exists('imagepng'))
{
// For PNG
header('Content-Type: image/png');
imagepng($im);
}
elseif(function_exists('imagewbmp'))
{
// For WBMP
header('Content-Type: image/vnd.wap.wbmp');
imagewbmp($im);
}
else
{
imagedestroy($im);
die('No image support in this PHP server');
}
// If image support was found for one of these
// formats, then free it from memory
if($im)
{
imagedestroy($im);
}
?>
Note:
As of PHP 4.0.2 you can use the function imagetypes() in place of function_exists() for checking the presence of the various supported image formats:
<?php
if(imagetypes() & IMG_GIF)
{
header('Content-Type: image/gif');
imagegif($im);
}
elseif(imagetypes() & IMG_JPG)
{
/* ... etc. */
}
?>