imagecreatefrompng

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

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

说明

imagecreatefrompng(string $filename): GdImage|false

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

小技巧

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

参数

filename

PNG 图像的路径。

返回值

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

更新日志

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

范例

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

<?php
function LoadPNG($imgname)
{
/* Attempt to open */
$im = @imagecreatefrompng($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/png');

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

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

以上例程的输出类似于:

imagecreatefrompng() 示例
add a note

User Contributed Notes

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