©
Ce document utilise Manuel du site Web PHP chinois Libérer
(PHP 4 >= 4.0.7, PHP 5, PHP 7)
imagegd2 — 将 GD2 图像输出到浏览器或文件
$image
[, string $filename
[, int $chunk_size
[, int $type
= IMG_GD2_RAW
]]] ) imagegd2() 将一个 GD 图像输出到
filename
。image
image
由图象创建函数(例如 imagecreatetruecolor() )返回的图象资源。
filename
文件保存的路径,如果未设置或为 NULL
,将会直接输出原始图象流。
chunk_size
Chunk size.
type
可以是
IMG_GD2_RAW
或 IMG_GD2_COMPRESSED
。默认为
IMG_GD2_RAW
。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 输出一个 GD2 图像
<?php
// Create a blank image and add some text
$im = imagecreatetruecolor ( 120 , 20 );
$text_color = imagecolorallocate ( $im , 233 , 14 , 91 );
imagestring ( $im , 1 , 5 , 5 , "A Simple Text String" , $text_color );
// Output the image
imagegd2 ( $im );
// Free up memory
imagedestroy ( $im );
?>
Example #2 保存 GD2 图像
<?php
// Create a blank image and add some text
$im = imagecreatetruecolor ( 120 , 20 );
$text_color = imagecolorallocate ( $im , 233 , 14 , 91 );
imagestring ( $im , 1 , 5 , 5 , "A Simple Text String" , $text_color );
// Save the gd2 image
// The file format for GD2 images is .gd2, see http://www.libgd.org/GdFileFormats
imagegd2 ( $im , 'simple.gd2' );
// Free up memory
imagedestroy ( $im );
?>
Note: 此函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。
Note:
GD2 格式一般是用来加载图像中的一部分时更快。注意 GD2 格式只能用于兼容于 GD2 的应用程序。
版本 | 说明 |
---|---|
4.3.2 |
添加了参数 chunk_size 和 type 。
|
[#1] Nick [2011-06-13 12:49:41]
You can use this function in combination with imagecreatefromstring() to clone the gd resource with minimum fuss (no writing to tmp file):
<?php
function cloneGd($gd)
{
ob_start();
imagegd2($gd);
return imagecreatefromstring(ob_get_clean());
}
?>
[#2] mark at teckis dot com [2003-04-16 06:33:14]
yes, the gd2 file format does improve the speed of image creations as the data-setup is designed to be native for the GD function - ie, the image doesn't have to be converted to a usable format prior to processing.
you may also note that the newer gd2 format creates much smaller size files than the older imagegd function, certainly for images involving chunks of single colours anyway. you'll probably find this function most useful for saving overlay images or background images used in larger image creation scripts.
to read a ping or jpeg image (.png / .jpg) and save a .gd2 version to server...
$img = $_GET['img'];
if(file_exists($img))
{
$dim = getimagesize($img);
$cr = ($dim[2] < 4) ? ($dim[2] < 3) ? ($dim[2] < 2) ? NULL : imagecreatefromjpeg($img) : imagecreatefrompng($img) : Null;
if($cr !== NULL)
{
imagegd2($cr,substr($img,0,strrpos($img,'.')).'.gd2');
}
}
should save a copy with the same filename and directory using extension .gd2 - which can then be nicely and swiftly read using either imagecreatefromgd2 or imagecreatefromgd2part