超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<ob_iconv_handlergd_info>
Last updated: Fri, 22 Jun 2007

XLIV. 图像函数库

简介

PHP 不仅限于只产生 HTML 的输出。还可以创建及操作多种不同图像格式的图像文件,包括 gif,png,jpg,wbmp 和 xpm。更方便的是,PHP 可以直接将图像流输出到浏览器。要处理图像,需要在编译 PHP 时加上图像函数的 GD 库。GD 和 PHP 还可能需要其它的库,视乎需要支持哪些图像格式而定。

可以在 PHP 中使用图像函数来得到 JPEGGIFPNGSWFTIFFJPEG2000 图像的大小。

注: 阅读需求一节以了解怎样扩展图像能力来读取,写入和修改图像以及读取数码相机拍摄的照片中的元数据。

需求

如果有 GD 库(在 http://www.boutell.com/gd/),你可以创建及操作图像。

所能处理的图像格式取决于你所安装的 GD 版本,以及其它 GD 可能用到的来访问这些图像格式的库。低于 gd-1.6 版本的 GD 支持 GIF 图像格式,不支持 PNG,高于 gd-1.6 的版本支持 PNG,不支持 GIF。

注: 自 PHP 4.3 起绑定了一个 GD 库的版本。这个绑定的版本具有一些附加的特色例如 alpha blending,应优先使用此版本,因为它的代码维护得更好以及更加稳定。

你可能想要增加 GD 处理的图像格式。

表格 1. 所支持的图像格式

图像格式要下载的库注解
gif  仅在低于 gd-1.6 的 GD 版本中支持。PHP 4.3.0 以及绑定的 GD 库中支持只读的 GIF 。
jpeg-6bftp://ftp.uu.net/graphics/jpeg/ 
pnghttp://www.libpng.org/pub/png/libpng.html 仅在高于 gd-1.6 的 GD 版本中支持。
xpmftp://metalab.unc.edu/pub/Linux/libs/X/!INDEX.html 如果你的系统中安装了 X 环境,则很可能已经有了这个库。

你可能想要增强 GD 处理不同字体的能力。支持下列字库:

表格 2. 所支持的字库

字库下载位置注解
FreeType 1.xhttp://www.freetype.org/ 
FreeType 2http://www.freetype.org/ 
T1libftp://sunsite.unc.edu/pub/Linux/libs/graphics/) 支持 Type 1 字体。

如果 PHP 编译时指定了 --enable-exif,就可以操作存储于 JPEGTIFF 图像文件头中的信息。这样就可以读取上面提到的数码相机生成的照片中的元数据。这些函数不需要 GD 库。

注: PHP 不需要任何附加的库来支持 exif 模块。

安装

要激活 GD 支持,配置 PHP 时加上 --with-gd[=DIR],DIR 是 GD 的基本安装目录。要使用推荐的绑定的 GD 库版本(首次绑定于 PHP 4.3.0),使用 --with-gd。在 Windows 下,要在 php.ini 中把 GD2 DLL php_gd2.dll 加到扩展库中。GD1 DLL php_gd.dll 自 PHP 4.3.2 起已经移除了。此外注意首选的真彩色图像函数例如 imagecreatetruecolor(),需要 GD2。

要在 PHP 3 中禁止 GD 支持,在配置时加上 --without-gd

要增强 GD 的能力以处理更多的图像格式,在配置 PHP 时指定 --with-XXXX 的配置开关。

表格 3. 所支持的图像格式

图像格式配置开关
jpeg-6b 要激活 jpeg-6b 的支持,加上 --with-jpeg-dir=DIR.
png 要激活 png 的支持,加上 --with-png-dir=DIR。注意,libpng 需要 zlib library,因此配置中还要加上 --with-zlib-dir[=DIR]
xpm 要激活 xpm 的支持,加上 --with-xpm-dir=DIR。如果配置时提示找不到所需要的库,可以加上到 X11 库的路径。

要增强 GD 的能力以处理更多的字体,在配置 PHP 时指定 --with-XXXX 的配置开关。

表格 4. 所支持的字库

字库配置开关
FreeType 1.x 要激活 FreeType 1.x 的支持,加上 --with-ttf[=DIR]
FreeType 2 要激活 FreeType 2 的支持,加上 --with-freetype-dir=DIR
T1lib 要激活 T1lib(Type 1 字体),加上 --with-t1lib[=DIR]
本地 TrueType 字符串函数 要激活本地 TrueType 字符串函数的支持,加上 --enable-gd-native-ttf

运行时配置

这些函数的行为受到全局配置文件 php.ini 的影响。

mbstring 模块可用时,exif 支持用户注释中的 Unicode 和 JIS 字符编码的自动转换。这是通过先用指定字符集将注释解码,把结果再用另一个符合你的 HTTP 输出的字符集编码来实现的。

表格 5. Exif 配置选项

名称默认值可变范围
exif.encode_unicode"ISO-8859-15"PHP_INI_ALL
exif.decode_unicode_motorola"UCS-2BE"PHP_INI_ALL
exif.decode_unicode_intel"UCS-2LE"PHP_INI_ALL
exif.encode_jis""PHP_INI_ALL
exif.decode_jis_motorola"JIS"PHP_INI_ALL
exif.decode_jis_intel"JIS"PHP_INI_ALL
有关 PHP_INI_* 常量的更多细节和定义见 ini_set()

以下是该配置选项的简要解释。

exif.encode_unicode string

exif.encode_unicode 定义了 UNICODE 用户注释被处理的字符集。默认为 ISO-8859-15,可用于大多数非亚洲国家。本设置可以为空或者必须为一个 mbstring 所支持的编码。如果为空,则使用当前 mbstring 内部使用的编码。

exif.decode_unicode_motorola string

exif.decode_unicode_motorola 定义了 Unicode 编码的用户注释的图像内部字符集,如果图像是摩托罗拉字节顺序(big-endian)的话。本设置不能为空但可以指定一个 mbstring 支持的编码列表。默认为 UCS-2BE。

exif.decode_unicode_intel string

exif.decode_unicode_intel 定义了 Unicode 编码的用户注释的图像内部字符集,如果图像是英特尔字节顺序(little-endian)的话。本设置不能为空但可以指定一个 mbstring 支持的编码列表。默认为 UCS-2LE。

exif.encode_jis string

exif.encode_jis 定义了 JIS 用户注释被处理的字符集。默认为空值,迫使函数使用当前 mbstring 使用的内部编码。

exif.decode_jis_motorola string

exif.decode_jis_motorola 定义了 JIS 编码的用户注释的图像内部字符集,如果图像是摩托罗拉字节顺序(big-endian)的话。本设置不能为空但可以指定一个 mbstring 支持的编码列表。默认为 JIS。

exif.decode_jis_intel string

exif.decode_jis_intel 定义了 JIS 编码的用户注释的图像内部字符集,如果图像是英特尔字节顺序(litle-endian)的话。本设置不能为空但可以指定一个 mbstring 支持的编码列表。默认为 JIS。

资源类型

该扩展模块未定义任何资源类型。

预定义常量

由于这些常量是由该扩展模块定义的,因此只有在该扩展模块被编译到 PHP 中,或者在运行时被动态加载后,这些常量才有效。

IMG_GIF (integer)

IMG_JPG (integer)

IMG_JPEG (integer)

IMG_PNG (integer)

IMG_WBMP (integer)

IMG_XPM (integer)

IMG_COLOR_TILED (integer)

IMG_COLOR_STYLED (integer)

IMG_COLOR_BRUSHED (integer)

IMG_COLOR_STYLEDBRUSHED (integer)

IMG_COLOR_TRANSPARENT (integer)

IMG_ARC_ROUNDED (integer)

IMG_ARC_PIE (integer)

IMG_ARC_CHORD (integer)

IMG_ARC_NOFILL (integer)

IMG_ARC_EDGED (integer)

IMAGETYPE_GIF (integer)

IMAGETYPE_JPEG (integer)

IMAGETYPE_PNG (integer)

IMAGETYPE_SWF (integer)

IMAGETYPE_PSD (integer)

IMAGETYPE_BMP (integer)

IMAGETYPE_WBMP (integer)

IMAGETYPE_XBM (integer)

IMAGETYPE_TIFF_II (integer)

IMAGETYPE_TIFF_MM (integer)

IMAGETYPE_IFF (integer)

IMAGETYPE_JB2 (integer)

IMAGETYPE_JPC (integer)

IMAGETYPE_JP2 (integer)

IMAGETYPE_JPX (integer)

IMAGETYPE_SWC (integer)

例子 1. 用 PHP 创建 PNG 图像

<?php
   header
("Content-type: image/png");
  
$string = $_GET['text'];
  
$im    = imagecreatefrompng("images/button1.png");
  
$orange = imagecolorallocate($im, 220, 210, 60);
  
$px    = (imagesx($im) - 7.5 * strlen($string)) / 2;
  
imagestring($im, 3, $px, 9, $string, $orange);
  
imagepng($im);
  
imagedestroy($im);
?>
本例应该在一个具有类似:<img src="button.php?text=text"> 标签的页面中被调用。上述的 button.php 脚本会取得 "text" 字符串将其覆盖在原图上(本例中的 "images/button1.png")并输出作为结果的图像。用此方法可以很方便地修改按钮上的文字从而避免了每次都要新画一个按钮的图像。用此方法就可以动态生成了。

目录
gd_info -- 取得当前安装的 GD 库的信息
getimagesize -- 取得图像大小
image_type_to_extension --  Get file extension for image type
image_type_to_mime_type --  取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的图像类型的 MIME 类型
image2wbmp -- 以 WBMP 格式将图像输出到浏览器或文件
imagealphablending -- 设定图像的混色模式
imageantialias -- 是否使用 antialias 功能
imagearc -- 画椭圆弧
imagechar -- 水平地画一个字符
imagecharup -- 垂直地画一个字符
imagecolorallocate -- 为一幅图像分配颜色
imagecolorallocatealpha -- 为一幅图像分配颜色 + alpha
imagecolorat -- 取得某像素的颜色索引值
imagecolorclosest -- 取得与指定的颜色最接近的颜色的索引值
imagecolorclosestalpha -- 取得与指定的颜色 + alpha 最接近的颜色
imagecolorclosesthwb --  取得与给定颜色最接近的色度的黑白色的索引
imagecolordeallocate -- 取消图像颜色的分配
imagecolorexact -- 取得指定颜色的索引值
imagecolorexactalpha -- 取得指定的颜色 + alpha 的索引值
imagecolormatch --  使一个图像中调色板版本的颜色与真彩色版本更能匹配
imagecolorresolve --  取得指定颜色的索引值或有可能得到的最接近的替代值
imagecolorresolvealpha --  取得指定颜色 + alpha 的索引值或有可能得到的最接近的替代值
imagecolorset -- 给指定调色板索引设定颜色
imagecolorsforindex -- 取得某索引的颜色
imagecolorstotal -- 取得一幅图像的调色板中颜色的数目
imagecolortransparent -- 将某个颜色定义为透明色
imagecopy -- 拷贝图像的一部分
imagecopymerge -- 拷贝并合并图像的一部分
imagecopymergegray -- 用灰度拷贝并合并图像的一部分
imagecopyresampled -- 重采样拷贝部分图像并调整大小
imagecopyresized -- 拷贝部分图像并调整大小
imagecreate -- 新建一个基于调色板的图像
imagecreatefromgd2 -- 从 GD2 文件或 URL 新建一图像
imagecreatefromgd2part -- 从给定的 GD2 文件或 URL 中的部分新建一图像
imagecreatefromgd -- 从 GD 文件或 URL 新建一图像
imagecreatefromgif -- 从 GIF 文件或 URL 新建一图像
imagecreatefromjpeg -- 从 JPEG 文件或 URL 新建一图像
imagecreatefrompng -- 从 PNG 文件或 URL 新建一图像
imagecreatefromstring -- 从字符串中的图像流新建一图像
imagecreatefromwbmp -- 从 WBMP 文件或 URL 新建一图像
imagecreatefromxbm -- 从 XBM 文件或 URL 新建一图像
imagecreatefromxpm -- 从 XPM 文件或 URL 新建一图像
imagecreatetruecolor -- 新建一个真彩色图像
imagedashedline -- 画一虚线
imagedestroy -- 销毁一图像
imageellipse -- 画一个椭圆
imagefill -- 区域填充
imagefilledarc -- 画一椭圆弧且填充
imagefilledellipse -- 画一椭圆并填充
imagefilledpolygon -- 画一多边形并填充
imagefilledrectangle -- 画一矩形并填充
imagefilltoborder -- 区域填充到指定颜色的边界为止
imagefilter --  Applies a filter to an image
imagefontheight -- 取得字体高度
imagefontwidth -- 取得字体宽度
imageftbbox -- 取得使用了 FreeType 2 字体的文本的范围
imagefttext -- 使用 FreeType 2 字体将文本写入图像
imagegammacorrect -- 对 GD 图像应用 gamma 修正
imagegd2 -- 输出 GD2 图像
imagegd -- 将 GD 图像输出到浏览器或文件
imagegif -- 以 GIF 格式将图像输出到浏览器或文件
imageinterlace -- 激活或禁止隔行扫描
imageistruecolor -- 检查图像是否为真彩色图像
imagejpeg -- 以 JPEG 格式将图像输出到浏览器或文件
imagelayereffect --  Set the alpha blending flag to use the bundled libgd layering effects
imageline -- 画一条直线
imageloadfont -- 载入一新字体
imagepalettecopy -- 将调色板从一幅图像拷贝到另一幅
imagepng -- 以 PNG 格式将图像输出到浏览器或文件
imagepolygon -- 画一个多边形
imagepsbbox -- 取得使用 PostScript Type1 字体的文本的范围
imagepscopyfont --  拷贝一个已加载的字体以备更改
imagepsencodefont -- 改变字体中的字符编码矢量
imagepsextendfont -- 扩充或压缩字体
imagepsfreefont -- 释放一个 PostScript Type 1 字体所占用的内存
imagepsloadfont -- 从文件中加载一个 PostScript Type 1 字体
imagepsslantfont -- 倾斜某字体
imagepstext -- 用 PostScript Type1 字体把文本字符串画在图像上
imagerectangle -- 画一个矩形
imagerotate -- 用给定角度旋转图像
imagesavealpha --  设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息(与单一透明色相反)
imagesetbrush -- 设定画线用的画笔图像
imagesetpixel -- 画一个单一像素
imagesetstyle -- 设定画线的风格
imagesetthickness -- 设定画线的宽度
imagesettile -- 设定用于填充的贴图
imagestring -- 水平地画一行字符串
imagestringup -- 垂直地画一行字符串
imagesx -- 取得图像宽度
imagesy -- 取得图像高度
imagetruecolortopalette -- 将真彩色图像转换为调色板图像
imagettfbbox -- 取得使用 TrueType 字体的文本的范围
imagettftext -- 用 TrueType 字体向图像写入文本
imagetypes -- 返回当前 PHP 版本所支持的图像类型
imagewbmp -- 以 WBMP 格式将图像输出到浏览器或文件
imagexbm --  Output XBM image to browser or file
iptcembed -- 将二进制 IPTC 数据嵌入到一幅 JPEG 图像中
iptcparse --  将二进制 IPTC http://www.iptc.org/ 块解析为单个标记
jpeg2wbmp -- 将 JPEG 图像文件转换为 WBMP 图像文件
png2wbmp -- 将 PNG 图像文件转换为 WBMP 图像文件



add a note add a note User Contributed Notes
图像函数库
07-Mar-2001 03:29
When using ImageGif( $im ) it sends the output directly to the browser. to prevent this from happening and also to be able to save your new image to a database for example use ob_start(), ob_get_contents(), and ob_end_clean()

ex:
<?php
function ResizeGif( $image, $newWidth, $newHeight){

//Open the gif file to resize
$srcImage = ImageCreateFromGif( $image );

//obtain the original image Height and Width
$srcWidth  = ImageSX( $srcImage );
$srcHeight = ImageSY( $srcImage );


// the follwing portion of code checks to see if
// the width > height or if width < height
// if so it adjust accordingly to make sure the image
// stays smaller then the $newWidth and $newHeight

if( $srcWidth < $srcHeight ){

$destWidth  = $newWidth * $srcWidth/$srcHeight;
$destHeight = $newHeight;
}else{

$destWidth  = $newWidth;
$destHeight = $newHeight * $srcHeight/$srcWidth;
}


// creating the destination image with the new Width and Height
$destImage = imagecreate( $destWidth, $destHeight);

//copy the srcImage to the destImage
ImageCopyResized( $destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight );

//create the gif
ImageGif( $destImage );


//fre the memory used for the images
ImageDestroy( $srcImage  );
ImageDestroy( $destImage );

}

//save output to a buffer
ob_start();

//Resize image  ( will be stored in the buffer )
ResizeGif( "/where/image/is/image.gif", "150", "150");

//copy output buffer to string
$resizedImage = ob_get_contents();

//clear output buffer that was saved
ob_end_clean();


//write $resizedImage to Database, file , echo to browser whatever you need to do with it

Also do not put Header() between ob_start and ob_end_clean() because they will still be sent. $resizedImage is the resized image had there been no ob function calls ResizeGif() would have sent the output to the browser.I hope this helps some people with databases that want to store there image

Dave

<ob_iconv_handlergd_info>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22