©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PHP 4, PHP 5, PHP 7)
imagecreatefrompng — 由文件或 URL 创建一个新图象。
$filename
)imagecreatefrompng() 返回一图像标识符,代表了从给定的文件名取得的图像。
如已启用fopen 包装器,在此函数中, URL 可作为文件名。关于如何指定文件名详见 fopen() 。各种 wapper 的不同功能请参见 支持的协议和封装协议,注意其用法及其可提供的预定义变量。
filename
PNG 图像的路径。
成功后返回图象资源,失败后返回 FALSE
。
Example #1 处理创建 PNG 过程中的错误
<?php
function LoadPNG ( $imgname )
{
$im = @ imagecreatefrompng ( $imgname );
if(! $im )
{
$im = imagecreatetruecolor ( 150 , 30 );
$bgc = imagecolorallocate ( $im , 255 , 255 , 255 );
$tc = imagecolorallocate ( $im , 0 , 0 , 0 );
imagefilledrectangle ( $im , 0 , 0 , 150 , 30 , $bgc );
imagestring ( $im , 1 , 5 , 5 , 'Error loading ' . $imgname , $tc );
}
return $im ;
}
header ( 'Content-Type: image/png' );
$img = LoadPNG ( 'bogus.image' );
imagepng ( $img );
imagedestroy ( $img );
?>
以上例程的输出类似于:
Windows 版本的 PHP 在 4.3.0 版之前不支持通过此函数访问远程文件,即使已经启用 allow_url_fopen.
[#1] Cyrille37 [2015-10-13 23:00:46]
The image size (width x height) should not exceed INT_MAX ... It's still thrue on a 64 bits computer.
[#2] admin at wdfa dot co dot uk [2009-04-17 15:06:02]
When using imagecreatepng with alpha blending you will lose the blending.
To over come this use something like the following
<?php
$dstimage=imagecreatetruecolor($width,$height);
$srcimage=imagecreatefrompng($src);
imagecopyresampled($dstimage,$srcimage,0,0,0,0, $width,$height,$width,$height);
?>
Where $width and $height are the width and height of the $src image.
This will create a true colour image then copy the png image to this true colour image and retain alpha blending.
[#3] marcos at assl-site dot net [2007-02-28 12:23:27]
I had the same problem as jboyd1189 at yahoo dot com but I solve d it allocating more memory dynamically.
Usually the memory_limit var on php.ini is set to 8M. Unfortunately, the required amount of memory to manage a PNG image about 1000x1000 could be bigger !
The approach I used to solve the problem is:
1-Calculate the memory required by the image
2-Set the new memory_limit value
3-Create the PNG image and thumbnail
4-Restore the original value
1-The following value works for me:
$required_memory = Round($width * $height * $size['bits']);
Note that for JPEG the requirements are not the same:
http://es2.php.net/manual/en/function.imagecreatefromjpeg.php#60241
2-Use somthing like:
$new_limit=memory_get_usage() + $required_memory;
ini_set("memory_limit", $new_limit);
4-ini_restore ("memory_limit");
[#4] jboyd1189 at yahoo dot com [2006-07-05 14:49:23]
I was having a terrible time with the imagecreatefrompng function as it was working perfectly for one image and not at all for another. After many hours of frustration, I discovered that the problem was the image size (number of pixels). It appears that the maximum number of pixels this function will process is 1,040,000. So, be sure that the pixel resolution of the image (eg. 1040 x 1000) does not exceed this value.
[#5] [2004-06-07 04:14:13]
If you're trying to load a translucent png-24 image but are finding an absence of transparency (like it's black), you need to enable alpha channel AND save the setting. I'm new to GD and it took me almost two hours to figure this out.
<?php
$imgPng = imageCreateFromPng($strImagePath);
imageAlphaBlending($imgPng, true);
imageSaveAlpha($imgPng, true);
header("Content-type: image/png");
imagePng($imgPng);
?>