imagecreatefromgif

(PHP 4, PHP 5, PHP 7, PHP 8)

imagecreatefromgif由文件或 URL 创建一个新图象。

说明

imagecreatefromgif(string $filename): GdImage|false

imagecreatefromgif() 返回图像标识符,代表从指定文件名获得的图像。

警告

当读取 GIF 文件到内存中时,图像对象仅返回第一帧。图像的大小不一定是 getimagesize() 报告的大小。

小技巧

如已启用fopen 包装器,在此函数中, URL 可作为文件名。关于如何指定文件名详见 fopen()。各种 wapper 的不同功能请参见 支持的协议和封装协议,注意其用法及其可提供的预定义变量。

参数

filename

GIF 图像的路径。

返回值

成功后返回图象对象,失败后返回 false

更新日志

版本 说明
8.0.0 成功时,此函数现在返回 GDImage 实例;之前返回 resource

范例

示例 #1 在加载 GIF 期间处理错误的示例

<?php
function LoadGif($imgname)
{
/* Attempt to open */
$im = @imagecreatefromgif($imgname);

/* See if it failed */
if(!$im)
{
/* Create a blank image */
$im = imagecreatetruecolor (150, 30);
$bgc = imagecolorallocate ($im, 255, 255, 255);
$tc = imagecolorallocate ($im, 0, 0, 0);

imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);

/* Output an error message */
imagestring ($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}

return
$im;
}

header('Content-Type: image/gif');

$img = LoadGif('bogus.image');

imagegif($img);
imagedestroy($img);
?>

以上例程的输出类似于:

示例输出:在加载 GIF 期间处理错误的示例
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top