imagegif
(PHP 4, PHP 5, PHP 7)
imagegif - 将图像输出到浏览器或文件
描述
bool imagegif ( resource $image [, mixed $to ] )
imagegif()to
从图像中创建GIF文件image
。该image
参数是从imagecreate()或返回imagecreatefrom *功能。
除非使用imagecolortransparent()将图像制作为透明,否则图像格式将为GIF87a,在这种情况下,图像格式将为GIF89a。
参数
`image`
一个图像资源,由图像创建函数之一返回,如imagecreatetruecolor()。
to
路径或打开的流资源(在此函数返回后自动关闭)将文件保存到。如果没有设置或者NULL
,原始图像流将被直接输出。
返回值
返回TRUE
成功或失败时返回FALSE
。
更新日志
版 | 描述 |
---|---|
5.4.0 | 增加了对将流资源传递给to的支持。 |
例子
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!';
?>
注释
注意:GIF支持已从版本1.6中的GD库中移除,并在版本2.0.28中添加。这些功能在这些版本之间不可用。有关更多信息,请参阅?GD项目网站。以下代码片段允许您通过自动检测可用的GD支持类型来编写更多可移植的PHP应用程序。替换序列标题(“Content-Type:image / gif”); imagegif($ im);由更灵活的序列:<?php //创建一个新的图像实例$ im = imagecreatetruecolor(100,100); //在这里执行一些图像操作//处理输出if(function \ _exists('imagegif')){//对于GIF头部('Content-Type:image / gif'); imagegif($ IM); } elseif(function \ _exists('imagejpeg')){//对于JPEG标题('Content-Type:image / jpeg'); imagejpeg($ im,NULL,100); } elseif(function \ _exists('imagepng')){//对于PNG头('Content-Type:image / png'); imagepng($ IM); } elseif(function \ _exists('imagewbmp')){//对于WBMP头部('Content-Type:image / vnd.wap.wbmp'); imagewbmp($ IM); } else {imagedestroy($ im); 死('在这个PHP服务器没有图像支持'); } //如果为这些//格式之一找到了图片支持,然后从内存中释放它($ im){imagedestroy($ im); }?>
注意:您可以使用函数imagetypes()来检查是否存在各种支持的图像格式:<?php if(imagetypes()&IMG \ _GIF){header('Content-Type:image / gif'); imagegif($ IM); } elseif(imagetypes()&IMG \ _JPG){/ \ * ... etc. \ * /}?>
← imagegetclip
imagegrabscreen →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com