imagegd2
(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)
imagegd2 — 将 GD2 图像输出到浏览器或文件
说明
imagegd2(
GdImage
?string
int
int
): bool
GdImage
$image
,?string
$file
= null
,int
$chunk_size
= 128,int
$mode
= IMG_GD2_RAW
): bool
输出 GD2 图像到指定 file
。
参数
-
image
由图象创建函数(例如imagecreatetruecolor())返回的 GdImage 对象。
file
-
文件保存的路径或者已打开的流资源(此方法返回后自动关闭该流资源),如果未设置或为
null
,将会直接输出原始图象流。 chunk_size
-
块大小。
mode
-
可以是
IMG_GD2_RAW
或IMG_GD2_COMPRESSED
。默认为IMG_GD2_RAW
。
示例
示例 #1 输出 GD2 图像
<?php
// 创建空白图像并添加一些文本
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
// 输出图像
imagegd2($im);
?>
示例 #2 保存 GD2 图像
<?php
// 创建空白图像并添加一些文本
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
// 保存 gd2 图像
// GD2 图像的文件格式为 .gd2,参阅 http://www.libgd.org/GdFileFormats
imagegd2($im, 'simple.gd2');
?>
注释
注意:
GD2 格式通常用于允许快速加载部分图像。请注意,GD2 格式仅可用于与 GD2 兼容的应用程序。
警告
The GD and GD2 image formats are proprietary image formats of libgd. They have to be regarded obsolete, and should only be used for development and testing purposes.
参见
- imagegd() - 将 GD 图像输出到浏览器或文件
+添加备注
用户贡献的备注 2 notes
Nick ¶
13 years ago
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());
}
?>
mark at teckis dot com ¶
21 years ago
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