利用PHP GD库生成位图

来源:互联网 发布:linux 文件用户组 编辑:程序博客网 时间:2024/06/16 06:26

关于PHP在apache中的安装配置请参考 Apache 配置CGI脚本操作数据库

位图

位图图像(bitmap), 亦称为点阵图像或绘制图像,是由称作像素(图片元素)的单个点组成的。这些点可以进行不同的排列和染色以构成图样。当放大位图时,可以看见赖以构成整个图像的无数单个方块。扩大位图尺寸的效果是增大单个像素,从而使线条和形状显得参差不齐。然而,如果从稍远的位置观看它,位图图像的颜色和形状又显得是连续的。

PHP与GD

PHP is not limited to creating just HTML output. It can also be used to create and manipulate image files in a variety of different image formats, including GIF, PNG, JPEG, WBMP, and XPM. Even more conveniently, PHP can output image streams directly to a browser. You will need to compile PHP with the GD library of image functions for this to work. GD and PHP may also require other libraries, depending on which image formats you want to work with. 此段意思是说PHP不只限于HTML输出,也可以在借助于GD库扩展的情况下创建和处理多种格式的图片并且将它们输出到浏览器中。在网站上GD库通常用来生成缩略图,或者用来对图片加水印,或者用来生成汉字验证码,或者对网站数据生成报表等。在PHP处理图像,可使用GD库,而GD库开始时是支持GIF的,但由于GIF使用了有版权争议的LZW算法,会引起法律问题,于是从 GD 库 1.6 版起所有的 GIF 支持都移除了,但是又在 GD 库 2.0.28 版起又加了回来。如果使用二者之间版本的 GD 库时 GIF 相关函数不可用。GD supports a varity of formats, below is a list of formats supported by GD and notes to their availability including read/write support. 

这里写图片描述
一般情况下在安装了php之后系统不会默认安装PHP GD库,所以我们需要执行以下命令来安装GD库

[root@lockey html]# yum install php-gd -y

这里写图片描述

安装完成之后我们就可以使用GD库来生成位图了,好吧,先上程序:

[root@lockey html]# cat weitu.php <?php// Requires the GD Libraryheader("Content-type: image/png");$im = imagecreatetruecolor(512, 512)    or die("Cannot Initialize new GD image stream");$white = imagecolorallocate($im, 255, 255, 255);for ($y = 0; $y < 512; $y++) {    for ($x = 0; $x < 512; $x++) {        if (rand(0, 1)) {            imagesetpixel($im, $x, $y, $white);        }    }}       imagepng($im);imagedestroy($im);?>[root@lockey html]# 

运行结果如下:
这里写图片描述
拓展一点:
在 Windows 中,需要将 GD2 的 DLL 文件 php_gd2.dll 作为一个扩展包含在 php.ini 中。GD1 的 DLL 文件 php_gd.dll 在 PHP 4.3.2 中被删除了。此外要注意首选的真彩色图像函数,例如 imagecreatetruecolor(),需要 GD2。

Note:要在 Windows 下启用 exif,在 php.ini 中 php_mbstring.dll 必须在 php_exif.dll 之前加载。

GD和图像处理函数:

gd_info — 取得当前安装的 GD 库的信息getimagesize — 取得图像大小getimagesizefromstring — 从字符串中获取图像尺寸信息image_type_to_extension — 取得图像类型的文件后缀image_type_to_mime_type — 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的图像类型的 MIME 类型image2wbmp — 以 WBMP 格式将图像输出到浏览器或文件imageaffine — 返回经过仿射变换后的图像,剪切区域可选imageaffinematrixconcat — Concatenate two affine transformation matricesimageaffinematrixget — Get an affine transformation matriximagealphablending — 设定图像的混色模式imageantialias — 是否使用抗锯齿(antialias)功能imagearc — 画椭圆弧imagebmp — Output a BMP image to browser or fileimagechar — 水平地画一个字符imagecharup — 垂直地画一个字符imagecolorallocate — 为一幅图像分配颜色imagecolorallocatealpha — 为一幅图像分配颜色 + alphaimagecolorat — 取得某像素的颜色索引值imagecolorclosest — 取得与指定的颜色最接近的颜色的索引值imagecolorclosestalpha — 取得与指定的颜色加透明度最接近的颜色imagecolorclosesthwb — 取得与给定颜色最接近的色度的黑白色的索引imagecolordeallocate — 取消图像颜色的分配imagecolorexact — 取得指定颜色的索引值imagecolorexactalpha — 取得指定的颜色加透明度的索引值imagecolormatch — 使一个图像中调色板版本的颜色与真彩色版本更能匹配imagecolorresolve — 取得指定颜色的索引值或有可能得到的最接近的替代值imagecolorresolvealpha — 取得指定颜色 + alpha 的索引值或有可能得到的最接近的替代值imagecolorset — 给指定调色板索引设定颜色imagecolorsforindex — 取得某索引的颜色imagecolorstotal — 取得一幅图像的调色板中颜色的数目imagecolortransparent — 将某个颜色定义为透明色imageconvolution — 用系数 div 和 offset 申请一个 3x3 的卷积矩阵imagecopy — 拷贝图像的一部分imagecopymerge — 拷贝并合并图像的一部分imagecopymergegray — 用灰度拷贝并合并图像的一部分imagecopyresampled — 重采样拷贝部分图像并调整大小imagecopyresized — 拷贝部分图像并调整大小imagecreate — 新建一个基于调色板的图像imagecreatefrombmp — 由文件或 URL 创建一个新图象。imagecreatefromgd2 — 从 GD2 文件或 URL 新建一图像imagecreatefromgd2part — 从给定的 GD2 文件或 URL 中的部分新建一图像imagecreatefromgd — 从 GD 文件或 URL 新建一图像imagecreatefromgif — 由文件或 URL 创建一个新图象。imagecreatefromjpeg — 由文件或 URL 创建一个新图象。imagecreatefrompng — 由文件或 URL 创建一个新图象。imagecreatefromstring — 从字符串中的图像流新建一图像imagecreatefromwbmp — 由文件或 URL 创建一个新图象。imagecreatefromwebp — 由文件或 URL 创建一个新图象。imagecreatefromxbm — 由文件或 URL 创建一个新图象。imagecreatefromxpm — 由文件或 URL 创建一个新图象。imagecreatetruecolor — 新建一个真彩色图像imagecrop — Crop an image to the given rectangleimagecropauto — Crop an image automatically using one of the available modesimagedashedline — 画一虚线imagedestroy — 销毁一图像imageellipse — 画一个椭圆imagefill — 区域填充imagefilledarc — 画一椭圆弧且填充imagefilledellipse — 画一椭圆并填充imagefilledpolygon — 画一多边形并填充imagefilledrectangle — 画一矩形并填充imagefilltoborder — 区域填充到指定颜色的边界为止imagefilter — 对图像使用过滤器imageflip — Flips an image using a given modeimagefontheight — 取得字体高度imagefontwidth — 取得字体宽度imageftbbox — 给出一个使用 FreeType 2 字体的文本框imagefttext — 使用 FreeType 2 字体将文本写入图像imagegammacorrect — 对 GD 图像应用 gamma 修正imagegd2 — 将 GD2 图像输出到浏览器或文件imagegd — 将 GD 图像输出到浏览器或文件imagegetclip — Get the clipping rectangleimagegif — 输出图象到浏览器或文件。imagegrabscreen — Captures the whole screenimagegrabwindow — Captures a windowimageinterlace — 激活或禁止隔行扫描imageistruecolor — 检查图像是否为真彩色图像imagejpeg — 输出图象到浏览器或文件。imagelayereffect — 设定 alpha 混色标志以使用绑定的 libgd 分层效果imageline — 画一条线段imageloadfont — 载入一新字体imageopenpolygon — Draws an open polygonimagepalettecopy — 将调色板从一幅图像拷贝到另一幅imagepalettetotruecolor — Converts a palette based image to true colorimagepng — 以 PNG 格式将图像输出到浏览器或文件imagepolygon — 画一个多边形imagepsbbox — 给出一个使用 PostScript Type1 字体的文本方框imagepsencodefont — 改变字体中的字符编码矢量imagepsextendfont — 扩充或精简字体imagepsfreefont — 释放一个 PostScript Type 1 字体所占用的内存imagepsloadfont — 从文件中加载一个 PostScript Type 1 字体imagepsslantfont — 倾斜某字体imagepstext — 用 PostScript Type1 字体把文本字符串画在图像上imagerectangle — 画一个矩形imageresolution — Get or set the resolution of the imageimagerotate — 用给定角度旋转图像imagesavealpha — 设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息(与单一透明色相反)imagescale — Scale an image using the given new width and heightimagesetbrush — 设定画线用的画笔图像imagesetclip — Set the clipping rectangleimagesetinterpolation — Set the interpolation methodimagesetpixel — 画一个单一像素imagesetstyle — 设定画线的风格imagesetthickness — 设定画线的宽度imagesettile — 设定用于填充的贴图imagestring — 水平地画一行字符串imagestringup — 垂直地画一行字符串imagesx — 取得图像宽度imagesy — 取得图像高度imagetruecolortopalette — 将真彩色图像转换为调色板图像imagettfbbox — 取得使用 TrueType 字体的文本的范围imagettftext — 用 TrueType 字体向图像写入文本imagetypes — 返回当前 PHP 版本所支持的图像类型imagewbmp — 以 WBMP 格式将图像输出到浏览器或文件imagewebp — 将 WebP 格式的图像输出到浏览器或文件imagexbm — 将 XBM 图像输出到浏览器或文件iptcembed — 将二进制 IPTC 数据嵌入到一幅 JPEG 图像中iptcparse — 将二进制 IPTC 块解析为单个标记jpeg2wbmp — 将 JPEG 图像文件转换为 WBMP 图像文件png2wbmp — 将 PNG 图像文件转换为 WBMP 图像文件
原创粉丝点击